以前のRailsアプリケーションでhas_and_belongs_to_many関連付けを使用していましたが、新しい、現在のアプリケーションではhas_many、:throughを使用しています。しかし、私はhas_manyの中心的なものを失っていると思っています。:Rails 3の関連付けを通して。Rails 3のトラブル:has_many:through associationとviews/forms
現在私は町のボランティア消防署用のアプリケーションを構築しています。会議があるときは、消防士が出席しているかどうか、出席していないかどうかをチェックしたい。
マイモデル:
#FireFighter Model
class FireFighter < ActiveRecord::Base
has_many :attendance_meetings
has_many :meetings, :through => :attendance_meetings
accepts_nested_attributes_for :meeting
#Meeting Model
class Meeting < ActiveRecord::Base
has_many :attendance_meetings
has_many :fire_fighters, :through => :attendance_meetings
accepts_nested_attributes_for :fire_fighter
#AttendanceMeeting Model
class AttendanceMeeting < ActiveRecord::Base
attr_accessor :status # this is the added property on the join model that we need populated
belongs_to :fire_fighter
belongs_to :meeting
私がいる問題は、ビューでこれを設定する方法です。現在、私が持っている:
= hidden_field_tag "meeting[fire_fighter_ids][]"
[email protected]_fighters.each do |fire_fighter|
= fire_fighter.fire_fighter_name
%span.radio_btn= radio_button_tag :fire_figher_ids, fire_fighter.id, "present"
present
%span.radio_btn= radio_button_tag :fire_figher_ids, fire_fighter.id, "excused"
excused
%span.radio_btn= radio_button_tag :fire_figher_ids, fire_fighter.id, "absent"
absent
%br
これは、(現在のオプション、免除、または不在で)各消防士のための各消防戦闘機と3つのラジオボタンを吐き出すます。ただし、結果のラジオボタンの名前はすべて同じであるため、すべての消防隊員に対して1つしか選択できません。
私が上記のように、私は何か基本的なものが欠けていると感じますが、私は困惑しています。私は数多くの質問と、ActiveRecordのThe Rails 3 Way bookのセクションを読んだ。どのような提案や指示をいただければ幸いです。ありがとうございました!
ありがとうございます!私はその応答に感謝し、今このアプローチを試しています。私はフォーム(fire_fighters v。attendance_meeting)で間違った関連付けに行くと思っていた。 –
残念ながら、それは私のために働いていませんが、私は私のモデルには何かがあります。 –
どのようなエラーが表示されますか? – mark