Back to articles
How to Use ActiveRecord Without Rails
How-ToSystems

How to Use ActiveRecord Without Rails

via Dev.to TutorialZil Norvilis

Very often I find myself writing small Ruby scripts to scrape data, process CSV files, or build a tiny Sinatra API. In these situations, generating a massive Rails application with ActionMailer, ActionCable, and Webpack is just overkill. But at the same time, I really don't want to write raw SQL queries by hand. I want the magic of ActiveRecord ( User.where(active: true) ), just without the heavy Rails framework attached to it. The good news is that Rails is basically just a collection of independent gems. You can completely rip ActiveRecord out of Rails and use it in a plain Ruby script. It only takes a few steps. STEP 1: The Dependencies First off, let's create a new directory for our script and create a Gemfile . We only need two gems: ActiveRecord itself, and a database adapter (I will use SQLite for this example to keep it simple). # Gemfile source 'https://rubygems.org' gem 'activerecord' gem 'sqlite3' Run bundle install in your terminal. STEP 2: The Connection Create a new file

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles