动态添加到collection_select

时间:2014-08-15 15:36:17

标签: ruby-on-rails ruby-on-rails-4 collection-select

我有3个模型:课程,单元和计划。 关系就像:

Course has_many units
Unit belongs_to course

Plan has_and_belongs_to_many units
Unit has_and_belongs_to_many plans

在create_plan页面中,我有一个collection_select来获取我的计划单位; 但是单位没有头衔,他们会使用他们的课程名称。 我想在collection_select中显示课程标题。 我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

将课程名称委托给

class Unit
  belongs_to course
  delegate :title, to: :course
end

<%= f.collection_select(:unit_id, Unit.all, :id, :title, prompt: true) %>