Skip to main content
Multilingual TursoDB, MVCC & AI Agent database guides Read Engine Guide →
Turso Cloud SDKs Official SDKs ActiveRecord Guides

Ruby on Rails + Turso

Set up Turso in your Ruby on Rails project in minutes.

WARNING

This is currently in technical preview. Join us in Discord to report any issues.

Prerequisites

Before you start, make sure you:

1. Install the libsql_activerecord Rubygem

Add the following to your Gemfile:

gem 'libsql_activerecord'

Then run:

bundle install

2. Configure database credentials

Get the database URL:

turso db show --url <database-name>

Get the database authentication token:

turso db tokens create <database-name>

Assign credentials to the environment variables inside .env.

TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=

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

3. Use the libsql adapter

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:

rails generate migration CreateProducts name:string description:text

Run the migration:

rails db:migrate

5. Define models

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

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:

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:

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

Examples

Synced from official Turso documentation ( tursodatabase/turso-docs · MIT License) Official Docs ↗
Search
Press Esc to close · Powered by Pagefind