ComponentItems 테이블을 통해 Items와 해당 구성 요소 사이에 has_many 연결이 있습니다. ComponentItems에는 item_id
및 component_id
외에도 quantity
열이 포함되어 있습니다. 각 폼의 숫자를 item
에 필요한 component
수량을 나타내는 number_field를 어떻게 추가 할 수 있습니까? 관계가없는 경우에도 (즉, @item.component_ids.empty? == true
) 양식에 데이터베이스의 각 항목에 number_field가 있어야합니다.레일 4는 추가 열을 통해 has_many를 설정합니다.
class Item < ActiveRecord::Base
has_many :components, through: :component_items
has_many :component_items
end
class Component < Item
has_many :items, through: :component_items
has_many :component_items
end
class ComponentItem < ActiveRecord::Base
belongs_to :item
belongs_to :component
end
저는 올바른 모델을 제외한 모델, 컨트롤러 및 form_builder의 모든 순열을 시도했다고 생각합니다.
아래의 답변에 대한 응답으로 하나의 특정 항목을 구성하는 구성 요소 항목의 확인란과 항목 코드가 표시된 양식이 있습니다.
<%= form_for [@item] do |f| %>
<%= f.collection_check_boxes :component_items, Item.active.where.not(sku: @item.sku).sort_by{|n| n.sku}, :id, :sku do |b| %>
<%= b.check_box %> <%= b.label %><br/>
<% end %>
<% end %>
따라서 check_box를 quantity_field로 바꾸는 것이 이상적입니다. 방법?
i.component_items.to_a.map (& : 수량)'은 각 구성 요소 항목에 대한 수량 배열을 제공하지만 양식 생성에 도움이되지 않습니다. 질문에 예제를 추가했습니다. – mvanio
죄송합니다. 정확히 무엇이 필요한지 이해하지 못했습니다. item.components의 모든 구성 요소에 대한 수량 번호를 표시 할 필드가 필요합니다. 맞습니까? –
거의. 모든 품목에 대해 수량을 표시해야합니다. 구성 요소의 경우이 값은 1 이상이어야하고 다른 항목의 경우 0이어야합니다. – mvanio