所以我有这种关联关系:Rails。 HAS_MANY:通过和的form_for PARAMS一个复选框字段
class FirstModel
has_many :merged_models
has_many :second_models, :through => :merged_models
end
class SecondModel
has_many :merged_models
has_many :first_models, :through => :merged_models
end
class MergedModel
belongs_to :first_model
belongs_to :second_model
end
现在我的问题是要了解这一招,帮助帮助识别元素在HTML从传递的集合在我形式:
form_for(first_model) do |f|
<% SecondModel.all.each do |s| -%>
<div>
<%= check_box_tag 'second_model_ids[]', s.id, first_model.second_models.include?(s), :name => 'first_model[second_model_ids][]'-%>
<%= label_tag :second_model_ids, s.first_name -%>
</div>
<% end -%>
我不明白的是:
first_model.second_models.include?(s), :name => 'first_model[second_model_ids][]'
我相信这一点:
first_model.second_models.include?(s)
检查SecondModel的对象ID已在FirstModel的second_model_ids
阵列。在这种情况下,我希望类似的if语句 - 如果此ID是有那么做,等
这部分让我更糊涂了:
:name => 'first_model[second_model_ids][]'
如果这一:name
是从哪里来的?为什么first_model[second_model_ids][]
有两个方括号 - 它们在Rails的语法是如何工作的?要合并这个新检查的ID给second_model_ids
阵列?
,我将感谢所有信息。谢谢!