has_many如何获得中间对象?

时间:2013-09-27 10:07:17

标签: ruby-on-rails ruby-on-rails-3

我有三个模特

JobRole JobRoleCompetencies Competencies

class JobRole
  has_many :job_role_competencies
  has_many :competencies, :through => :job_role_competencies
end

JobRoleCompetencies还有其他字段 - min_rate

当我创建新模型时,我分配了JobRole.new(competency_ids: [1,2])等能力 之后我想分配最小'速率'但是当我检查JobRole.new(competency_ids: [1,2]).job_role_competencies - 什么都没有 - 我怎样才能得到(或构建)这个中间对象。

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?

@job_role = JobRole.new
[1, 2].each do |competency_id|
  @job_role.job_role_competencies.build(:min_rate => min_rate, :competency_id => competency_id)
end