Rails has_many_through migration

妖精的绣舞 提交于 2019-12-25 02:07:00

问题


I'm creating a many_to_many association for the following models:

class Competence < ActiveRecord::Base
    has_many :behaviour, through: :behaviours_rel
    has_many :stabilizer, through: :stabilizers_rel
end

class Behaviour < ActiveRecord::Base
    belongs_to :competence
end

class Stabilizer < ActiveRecord::Base
    belongs_to :competence
end

I believe I have to do something like:

rails generate migration behaviour:belongs_to 

but it doesn't work. I simply can't get the logic of doing this migration with rails generate.

I'm trying to save in a competence several behaviours so a one_to_many relationship is not enough.

By the way, I don't want to do it explicitly with sql table.


回答1:


$ rails g model behaviours_rel competence_id:integer behaviour_id:integer

$ rails g model stabilizers_rel competence_id:integer stabilizer_id:integer

$ bundle exec rake db:migrate

See: http://www.codequizzes.com/learn-rails/many-to-many-relationships



来源:https://stackoverflow.com/questions/29113046/rails-has-many-through-migration

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