Jed Schneider
Jed Schneider

Reputation: 14671

Using ActiveRecord models in a gem - how to handle database config

I have several active record models in a rails app and I would like to extract these models into a gem so that I can easily use them in several web apps. The process seems pretty straight forward, except for passing along the configuration for the models. Do I:

  1. Add the configuration yaml file to the gem, thus assuring the databases will always be the same across all apps - seems rigid, esp for testing and dev, though the databases for production will always be consistent.
  2. Use the ActiveRecord hooks to look for a database.yml file in the config directory with the database defined? If so, which hooks should I use?
  3. This is a stupid idea. If you have a better way to handle this, I'm all ears. I'd prefer not to copy and paste.

Upvotes: 2

Views: 1639

Answers (2)

Andrew Vit
Andrew Vit

Reputation: 19249

You should use the host rails app's database config. Your plugin or gem should contain just the database migrations, and a rake task to run them from the host rails app (e.g. myplugin:db:migrate)

If your models need some other configuration file, you should create a rake task (e.g. myplugin:install) to copy it to your host app's config directory. (This task can call the db:migrate task automatically as well.)

Upvotes: 3

Ashwin Phatak
Ashwin Phatak

Reputation: 655

Why do you want to embed the database.yml file inside the gem? Each rails application should use it's own database.yml

I would put all the models into a plugin and include that in each rails application that needs the models.

Upvotes: 1

Related Questions