There are plenty of tips out there for using ActiveRecord without Rails, but I was having trouble synthesizing all of the relevant information. Once I got things working, I decided to bring together all of the important pieces I used.
Steps, for an imaginary app called MyApp:
-
Make directories
mkdir myapp mkdir myapp/lib mkdir myapp/db mkdir myapp/config
-
Create Gemfile
bundle init
- edit accordingly, making sure to include the gems listed in the Gemfile
bundle install
- Create Rakefile as shown below
- Create your database
rake db:create
-
Create your migration(s) using the syntax described here
rake db:new_migration name=CreateUsers options="attr_1:type attr_2:type attr_3:type" rake db:migrate
-
Create your models
touch lib/models/<model_name>.rb
- be sure to inherit from ActiveRecord, e.g.
class User < ActiveRecord::Base
- Make sure to require the
environment.rb
file in whatever script you initiate - You're good to go!
Sources: