我有以下型号
class Outbreak < ActiveRecord::Base
has_many :risks
has_many :factors, :through => :risks
end
class Risk < ActiveRecord::Base
belongs_to :outbreak
belongs_to :factor
end
class Factor < ActiveRecord::Base
has_many :risks
has_many :outbreaks, :through => :risks
end
Risk table
id : integer
outbreak_id : integer
factor_id : integer
details : text
在视图/爆发/编辑
中 <div id="factor_div">
<% for factor in Factor.find(:all, :conditions => {:outbreak_type => @outbreak.outbreak_type}) %>
<div>
<%= check_box_tag "outbreak[factor_ids][]", factor.id , @outbreak.factors.include?(factor) %>
<%= factor.name %>
<%= text_field_tag "risk[details][]", @outbreak.risks.each{ |risk| if risk.factor_id == factor.id; risk} %>
</div>
<% end %>
</div>
我正在尝试编辑Risk model details属性(并从@outbreak.risks数组中填充正确的text_field)。 是否有可能使用迭代通过因子(主要是静态变量的表 - 必须由最终用户偶尔改变)的方法,然后检查每个爆发.risk是否具有该factor_id或我是只是走错了路?
(想不到 - 周五下午,阳光明媚......以及路上的啤酒花园^^)。
答案 0 :(得分:0)
首先,更正关联并更新结果。
class Factor < ActiveRecord::Base
has_many :risks
has_many :outbreaks, :through => :risks
end