2

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.

2 Answers 2

3

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.)

1
  • yah, good call, like the cucumber gem adding the cucumber env to the db config file. I was going down this path but this helped me clarify the though. Thanks. Aug 23, 2010 at 17:31
1

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.

1
  • curious, why a plugin over a gem? One reason I considered embedding the database.yml file was because this is internal and the db is not changing, we could manage it all from one location - just update the gem. Also, what if I am using the models and the database connection outside a rails app, eg a command line app that is load testing the database. Aug 23, 2010 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.