# Turso Quickstart (ActiveRecord)

> Get started with Turso and ActiveRecord in a few simple steps.
> Canonical HTML: https://tursodb.cn/docs/sdk/activerecord/quickstart/
> Upstream Source (MIT): https://docs.turso.tech/sdk/activerecord/quickstart
> Language: en

---
<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

This is currently in technical preview. [Join us in Discord](https://tur.so/discord) to report any issues.

</div></div>

In this Ruby quickstart we will learn how to:

* Retrieve database credentials
* Install the libSQL ActiveRecord gem
* Connect to a local or remote Turso database
* Define and create models
* Perform basic CRUD operations
* Execute raw SQL queries
* Work with migrations

**1. Retrieve database credentials**

You will need an existing database to continue. If you don't have one, [create one](/docs/quickstart/).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

**2. Install**

In your Ruby project, add the following gems to your Gemfile:

```ruby
gem 'libsql_activerecord'
gem 'activerecord'
```

Then run:

```bash
bundle install
```

**3. Connect**

Create a Ruby file (e.g., `database.rb`) to set up the database connection:

<details class="doc-accordion"><summary>Embedded Replicas</summary><div class="doc-accordion-body">

```rb
require 'libsql_activerecord'
require 'active_record'

ActiveRecord::Base.establish_connection(
  adapter: 'libsql',
  url: ENV['TURSO_DATABASE_URL'],
  auth_token: ENV['TURSO_AUTH_TOKEN'],
  path: 'path/to/local/replica.db'
)
```

</div></details>


<details class="doc-accordion"><summary>Local only</summary><div class="doc-accordion-body">

```rb
require 'libsql_activerecord'
require 'active_record'

ActiveRecord::Base.establish_connection(
  adapter: 'libsql',
  path: 'path/to/local.db'
)
```

</div></details>


<details class="doc-accordion"><summary>Remote only</summary><div class="doc-accordion-body">

```rb
require 'libsql_activerecord'
require 'active_record'

ActiveRecord::Base.establish_connection(
  adapter: 'libsql',
  url: ENV['TURSO_DATABASE_URL'],
  auth_token: ENV['TURSO_AUTH_TOKEN']
)
```

</div></details>


<details class="doc-accordion"><summary>In-memory</summary><div class="doc-accordion-body">

```rb
require 'libsql_activerecord'
require 'active_record'

ActiveRecord::Base.establish_connection(adapter: 'libsql')
```

</div></details>


**4. Define models**

Create model files for your database tables. For example, `product.rb`:

```rb
class Product < ActiveRecord::Base
  validates :name, presence: true
end
```

**5. Create and execute a migration**

Create a migration file (e.g., `001_create_products.rb`):

```rb
class CreateProducts < ActiveRecord::Migration[8.0]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description
      t.timestamps
    end
  end
end
```

Execute the migration:

```rb
require_relative 'database'
require_relative '001_create_products'

CreateProducts.migrate(:up)
```

**6. Execute**

Perform some basic CRUD operations:

```rb Create
product = Product.create(name: 'Book', description: 'A book about books')
```

```rb Read
all_products = Product.all
first_product = Product.first
found_product = Product.find_by(name: 'Book')
```

```rb Update
product.update(description: 'An updated description')
```

```rb Delete
product.destroy
```

```rb Raw SQL
results = ActiveRecord::Base.connection.execute("SELECT * FROM products WHERE name LIKE '%Book%'")

results.each do |row|
  puts row.inspect
end
```

**7. Work with associations**

Define associations in your models:

```ruby
class Author < ActiveRecord::Base
  has_many :books
end

class Book < ActiveRecord::Base
  belongs_to :author
end
```

Use associations in your code:

```ruby
author = Author.create(name: 'Jane Doe')
book = author.books.create(title: 'My First Book')

puts author.books.count # => 1
puts book.author.name # => "Jane Doe"
```
