# Ruby on Rails + Turso

> Set up Turso in your Ruby on Rails project in minutes.
> Canonical HTML: https://tursodb.cn/docs/sdk/activerecord/guides/rails/
> Upstream Source (MIT): https://docs.turso.tech/sdk/activerecord/guides/rails
> 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>

## Prerequisites

Before you start, make sure you:

* [Install the Turso CLI](/docs/cli/installation/)
* [Sign up or login to Turso](/docs/cli/authentication/#signup)
* Have a Rails app — [learn more](https://guides.rubyonrails.org/getting_started.html)

**1. Install the libsql\_activerecord Rubygem**

Add the following to your Gemfile:

```ruby
gem 'libsql_activerecord'
```

Then run:

```bash
bundle install
```

**2. Configure database credentials**

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=
```

Add your Turso credentials to your Rails credentials file or environment variables.

**3. Use the libsql adapter**

```yml
default: &default
  adapter: libsql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  url: <%= ENV['TURSO_DATABASE_URL'] %>
  auth_token: <%= ENV['TURSO_AUTH_TOKEN'] %>
  path: storage/development.sqlite3 # For embedded replica
```

**4. Create and run migrations**

Generate a migration:

```bash
rails generate migration CreateProducts name:string description:text
```

Run the migration:

```bash
rails db:migrate
```

**5. Define models**

Create a model in `app/models/product.rb`:

```ruby
class Product < ApplicationRecord
  validates :name, presence: true
end
```

**6. Use in controllers**

In your controllers, you can now use ActiveRecord methods to interact with your Turso database:

```ruby
class ProductsController < ApplicationController
  def index
    @products = Product.all
  end

  def create
    @product = Product.new(product_params)
    if @product.save
      redirect_to @product, notice: 'Product was successfully created.'
    else
      render :new
    end
  end

  private

  def product_params
    params.require(:product).permit(:name, :description)
  end
end
```

**7. Execute raw SQL (if needed)**

You can also execute raw SQL queries:

```ruby
results = ActiveRecord::Base.connection.execute("SELECT * FROM products")
```

## Examples

<div class="doc-cards">
<a class="doc-card" href="https://github.com/tursodatabase/libsql-activerecord/tree/main/examples/railsapp" target="_blank" rel="noopener noreferrer"><div class="doc-card-title"><span>Ruby on Rails App</span><span class="doc-card-arrow" aria-hidden="true">↗</span></div><p class="doc-card-desc">See the full source code</p></a>
</div>
