2

I have three tables via many-to-many-association: Supermarket, Product and Supply. Each Supermarket can hold many products and each product can be sold in many supermarkets. The association is build via the Supply-model.

Supermarket:

class Supermarket < ActiveRecord::Base
  attr_accessible :name, :address, :products_attributes

  has_many :supplies
  has_many :products, :through => :supplies

  accepts_nested_attributes_for :products
end

Product:

class Product < ActiveRecord::Base
  attr_accessible :name, :supermarkets_attributes

  has_many :supplies
  has_many :supermarkets, :through => :supplies
  accepts_nested_attributes_for :supermarkets
end

Association via Supply:

class Supply < ActiveRecord::Base
  attr_accessible :supermarket_id, :product_id

  belongs_to :supermarket
  belongs_to :product
end

I have created the scaffolds and populated the Supermarket-table. In my Product form, i want to use one (or more) drop-down-menu(s) to select the correspondent Supermarket-name(s). Goal is to create a new product while also creating the association via the Supply-table. What should the code look like in form and/or controller for the products if I want to select the corresponding supermarkets from there?

c_ern
  • 151
  • 2
  • 11
  • So I can solve this via the `collection_select`-helper and it works just fine. But as being relatively new to rails, I want to know: what is the difference to using `nested forms` as described in [Railscast #196](http://railscasts.com/episodes/196-nested-model-form-part-1) - i.e. what are the (dis)advantages of using what and where do I use what? – c_ern Sep 14 '12 at 08:15

2 Answers2

4

In you products form you need to add this line...

<%= collection_select(:product, :supermarket_ids, SuperMarket.all, :id, :name, {}, { :multiple => true } )%>

You also shouldn't need to use an accepts_nested_attributes for this, the many to many association you already have set up should take care of the rest.

rocket scientist
  • 2,427
  • 1
  • 19
  • 28
  • That looks very promising: the form shows up, but when sending it I get an `ActiveModel::MassAssignmentSecurity::Error` Another sulution I tried is to use a `fields_for` but I got the same error. – c_ern Sep 13 '12 at 18:24
  • I came across something similar(I used this approach for skills and users). Try adding :supermarket_id to your attr_accessible list in products. – rocket scientist Sep 13 '12 at 18:30
  • Okay, that did the trick :-). I had some apprehension about security when adding it to the model, because the mass-assignment security-warning is certainly there for a reason - but skimming through [the mass-assignment rails-guide](http://guides.rubyonrails.org/security.html#mass-assignment) I think it's not more a risk than nested forms too. Probably someone here can give me further information... – c_ern Sep 13 '12 at 18:55
0

I think in views

<%= f.collection_select "super_market_ids[]",@super_markets,:id,:name,{},{:multiple=>"multiple'} %>

I am not sure about super_market_ids or super_market_ids[] and syntax just verified once.

In select tag if you want checkbox type multi select there is an chosen library that will help you to build nicer UI,

Amar
  • 6,874
  • 4
  • 25
  • 23
  • OK, I think collection_select is the way to go - but i want to access the **supermarkets** from the **products**-form. What variables do I have to use? – c_ern Sep 13 '12 at 13:13
  • in controller in products action edit and new `@super_markets = SuperMarket.all` – Amar Sep 13 '12 at 13:15
  • Sorry, it does not work - is super_market_ids something that rails does automagically? – c_ern Sep 13 '12 at 13:56
  • to be precise: i get a `NoMethodError` for any combination of `super_market_id` or `super_markets_id` or so on... – c_ern Sep 13 '12 at 14:19
  • watch this epidode it's similar http://railscasts.com/episodes/17-habtm-checkboxes?view=asciicast – Amar Sep 13 '12 at 14:27