所以我有這種關聯關係: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
陣列?
,我將感謝所有信息。謝謝!