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:
- Add the configuration yaml file to the gem, thus assuring the 开发者_StackOverflow中文版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.
- 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?
- 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.
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.)
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.
精彩评论