13

I'm building a Rails application based on hexagonal architecture.

One of my adapters is storage adapter (maintained as a gem) that manages access to database and provides simple interface for rails application to store and query data in database.

I'd like to use ActiveRecord in this gem with all rake tasks (create, migrate, drop, rollback) for managing database.

How can I use AR outside rails, but with all rake tasks?

3

2 Answers 2

18

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That's what I have so far. My problem is to have all rake tasks for managing database in my gem as well.
Here is a relevant github issue discussing your use case, with example code. github.com/rails/rails/issues/11609
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.