假设我有三个表,表一为 projects,表二为 polls,中间表为 projects_polls,我想通过 创建 project 时选择数组 poll,并将数据更新到中间表中 表一和表二是多对多的关联,has_and_belongs_to_many,我需要怎么做?求大神指点
class Project < ActiveRecord::Base
has_and_belongs_to_many :polls
end
class Poll < ActiveRecord::Base
has_and_belongs_to_many :projects
end
class ProjectsPolls < ActiveRecord::Base
unloadable
belongs_to :project
belongs_to :poll
end
project 表
create_table :projects do |t|
t.string :name
t.references :poll, index: true
t.timestamps null: false
end
poll 表
create_table :polls do |t|
t.string :name
t.timestamps null: false
end
中间表
create_table :projects_polls do |t|
t.references :project, index: true
t.references :poll, index: true
t.timestamps null: false
end