我通过a 在 a和 ahas_many_through
之间建立了关系。不同寻常的是,当我建立关联时,我需要为:Contributor
Resource
Contributorship
contribution_type
model Contributor
has_many contributorships
has_many contributors, through: contributorships
end
model Resource
has_many contributorships
has_many resources, through: contributorships
end
model Contributorships
attr_accessible :contribution_type, :contributor, :archive_resource
belongs_to resources
belongs_to contributors
end
建立关联涉及:
c = Contributor.create!()
r = Resource.create!()
c.contributorships.create!(resource:r, contribution_type: :author)
或者(如果我不想预先保存):
c = Contributor.new()
r = Resource.new()
cs = Contributorship.new(contributor:c, resource:r, contribution_type: :author)
c.save!
r.save!
cs.save!
如果我不需要contribution_type
在Contributorship
加入模型上设置属性,我可以这样做:
c.resources << r
那么有没有更优雅的方式来同时设置属性呢?