问题
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