Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Simple and easy way to use ActiveRecord without Rails

Posted on 2019-05-21 by Rodrigo Urubatan

After a question in my other post about how to use sidekiq without rails, I decided to also post here how to use ActiveRecord without Rails

But Just to simplify things a lot, I’ll show how to do it using only one file, then you can split it up in your projects.

require 'active_record'
ActiveRecord::Base.establish_connection({adapter: :sqlite3, database: 'test.db'})
 
 
 
class CreateTestModel < ActiveRecord::Migration[5.1]
  def change
    create_table :test_models do |t|
      t.string :name
    end
  end
end
 
CreateTestModel.migrate :up
 
class TestModel < ActiveRecord::Base
end
 
TestModel.create(name: 'First Test')
TestModel.create(name: 'Second Test')

As you can see, what you need is pretty simple, you just need to:

  • load the ‘active_record’ gem
  • establish the connection passing a hash with the configuration
  • running your migrations
  • creating your model classes
  • And using your models as you would normally in a Rails application

But how can we improve that?

You can merge this with the standalone sidekiq sample from the other post, split the files in their own directory instead of using only one file for everything, and just be happy loading only the gems you need!

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme