Model
class Rfq < ActiveRecord::Base
belongs_to :purchase
belongs_to :supplier
belongs_to :customtemplate
end
class Purchase < ActiveRecord::Base
belongs_to :order
#Products
has_many :purchase_products, dependent: :destroy
has_many :products, through: :purchase_products
accepts_nested_attributes_for :purchase_products, allow_destroy: true
validates :code, uniqueness: true
validates :code, presence: true
validates :order_id, presence: true
end
class Supplier < ActiveRecord::Base
has_many :products, dependent: :destroy
accepts_nested_attributes_for :products, allow_destroy: true
validates :code, uniqueness: true
validates :code, presence: true
end
class Customtemplate < ActiveRecord::Base
has_many :supplier_templates, dependent: :destroy, :foreign_key => 'customtemplate_id'
has_many :suppliers, through: :supplier_templates
validates :code, uniqueness: true
validates :code, :body, presence: true
end
rfqs_controller
class RfqsController < ApplicationController
(..)
def new
@rfq = Rfq.new
#@purchase_products = PurchaseProduct.where(purchase_id: params[:purchase_id])
#RFQ for one purchase
if params[:purchase_id].present?
@a_products_suppliers = []
products = Purchase.find(params[:purchase_id]).products
@suppliers = products.select(:supplier_id).uniq
products.each do |p|
@a_products_suppliers.push(p.supplier_id => p.id)
#binding.pry
end
end
binding.pry
end
(..)
One of the model fields is "products" that is defined as array type.
From the controller, in the @a_products_suppliers I receive array of hashes in the form of [{supplier_id=>product_id}]
(#<RfqsController>)> @a_products_suppliers
=> [{9=>5}, {9=>4}]
Form
<% @suppliers.each do |s| %>
<%= simple_form_for @rfq do |f| %>
<%= f.error_notification %>
<h3>Products</h3>
<% @a_products_suppliers.each do |a| %>
<%= f.collection_check_boxes a[s.supplier_id] ???
<% end %>
<%= f.button :submit %>
<% end %>
<% end %>
I can't figure out how to list each product allowing user to use checkboxes to define which products should be stored in the "products" column in the table rfqs.
Labels can be obtained by writting: Product.find(a[s.supplier_id]).code
Can sb help me with that?