How to install database schema from a Rails 3 plugin in another plugin

自闭症网瘾萝莉.ら 提交于 2019-12-13 07:23:59

问题


I'm writing a Rails 3 plugin that uses another Rails 3 plugin that I recently wrote. Let's call them July and August. So in August's gemspec, I add the line:

s.add_dependency "july", "~> 0.0.1"

and I run bundle install. Then I create some models and fixtures. Next I need to migrate the database:

$ cd test/dummy
$ rake august:install:migrations
$ rake db:migrate

Now, the August (the plugin I am creating) tables are in the development and test databases, but the July tables are not. But my August tables have foreign keys to my July tables, so before I can run any tests, I need to create the July tables and write appropriate fixtures. I would expect to run rake -T and see

rake august:install:migrations
rake july:install:migrations

but all I see is the august rake task. So how do I create the July database tables (other than creating a new migration, which would violate DRY since I've already done that in my July codebase)?


回答1:


The Dummy App has to be directly dependent on "july" for it to load "july"'s rake tasks. So I need to include it in the Gemfile, not the gempspec. However, putting it in Gemfile won't force real (non-dummy) apps to install july when I bundle install after putting "august" in their Gemfile/gemspec.

So I need to include it in both places--in the Gemfile for the rake task (and this can be in a :development group), and in the gemspec (using add_dependency or add_runtime_dependency) to force installation of the dependency. That's the solution, but I don't understand why.

I tried explaining my thoughts on this in my comment here but wasn't really able to pinpoint any logic that explains this design methodology. Could someone please explain the true reason?



来源:https://stackoverflow.com/questions/12518232/how-to-install-database-schema-from-a-rails-3-plugin-in-another-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!