How to use ActiveRecord outside Rails? How to use ActiveRecord outside Rails? ruby-on-rails ruby-on-rails

How to use ActiveRecord outside Rails?


Install it like any other gem

gem install activerecord

Then you configure it somewhere like this

ActiveRecord::Base.establish_connection(  :adapter  => 'mysql',  :database => 'database',  :username => 'user',  :password => 'password',  :host     => 'localhost')

Models can then inherit as normal from ActiveRecord::Base

You get all of the rake tasks but you do have to do some extra configuration since you will not have the Rails. Here is the link inside of activerecord for how to configure that stuff.

Database tasks


An updated solution is the standalone_migrations. Basically after doing the configuration for the gem and creating the configuration, you have access to all the ActiveRecord niceties. Rake tasks included!

Also, I made a small generator for scaffolding simple scripts that have access to ActiveRecord out of the box: nrb - Ninja Ruby.