1

コース、ユニット、プランの 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にコースタイトルを表示したい。どうやってやるの?

4

1 に答える 1

2

コースにタイトルを委任する

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

<%= f.collection_select(:unit_id, Unit.all, :id, :title, prompt: true) %>
于 2014-08-15T15:46:16.223 に答える