Rails has_many_through迁移
问题描述:
我正在为以下模型创建一个many_to_many关联:
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
但是它不起作用.我简直无法理解使用rails generate进行这种迁移的逻辑.
but it doesn't work. I simply can't get the logic of doing this migration with rails generate.
我正在尝试保留几种行为,以至于one_to_many关系还不够.
I'm trying to save in a competence several behaviours so a one_to_many relationship is not enough.
顺便说一句,我不想用sql表显式地完成它.
By the way, I don't want to do it explicitly with sql table.
答
$ 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