【发布时间】:2011-09-14 15:32:30
【问题描述】:
我以前在我的旧 Rails 应用程序中使用 has_and_belongs_to_many 关联,但现在正在转向使用 has_many, :through 来处理新的和当前的应用程序。但是,我相信我遗漏了 has_many 的核心内容,:通过 Rails 3 中的关联。
目前,我正在为我们镇的志愿消防部门构建一个应用程序。当我们开会时,我们想检查消防员是否在场、请假或缺席。
我的模型:
#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][]"
-@meeting.fire_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
这将显示每个消防员和每个消防员的三个单选按钮(可选择出席、请假或缺席)。但是,生成的单选按钮的名称都是相同的,因此您只能为所有消防员选择一个。
正如我上面提到的,我确信我错过了一些基本的东西,但我很难过。我已经阅读了大量的 SO 问题和关于 ActiveRecord 的 Rails 3 Way 书的部分。任何建议或指示将不胜感激。谢谢!
【问题讨论】:
标签: ruby-on-rails activerecord associations