0

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?

Michal
  • 139
  • 3
  • 14

1 Answers1

0

Please try something as below:

<%= collection_check_boxes(:student, :subject_ids, Student.order('name'), :id, :name) do |b| %>
   <%= b.check_box %>
   <%= b.label %>
<% end %>

Where I am having Student-Subject many-to-many relation. collection_check_box iterates in itself.

Let me know if you face any issue...

SnehaT
  • 309
  • 4
  • 14
  • I have seen that solution but it does not work for me. In my case there is no relation between models. Sth like below code does not work (:id, :code are fields from the Product model) `code`<% @a_products_suppliers.each do |a| %> <%= f.collection_check_boxes :rfq, :products, Product.find(a[s.supplier_id]), :id, :code <% end %>`code` – Michal Aug 10 '16 at 12:44
  • I have seen that solution but it does not work for me. In my case there is no relation between models. Sth like below code does not work (:id, :code are fields from the Product model) `<% @a_products_suppliers.each do |a| %> <%= f.collection_check_boxes :rfq, :products, Product.find(a[s.supplier_id]), :id, :code <% end %>` – Michal Aug 10 '16 at 12:50
  • Is that you want to list down suppliers for that product in the collection check box – SnehaT Aug 10 '16 at 12:59
  • By looping through @a_products_suppliers I want to list all products assigned to each supplier and by checkboxes to allow user to choose the final list of products that should be included in the rfq (request for quotation) – Michal Aug 10 '16 at 13:36
  • Can u please show ur rfq-Product-Supplier relation...what is this rfq? please use descriptive names, so its user readable and easy to understand too.. – SnehaT Aug 10 '16 at 13:46
  • I updated my question with definitions of all models that rfq model is associated – Michal Aug 10 '16 at 14:04
  • I am adding also part of rfqs_controller – Michal Aug 10 '16 at 14:10