0

I am hoping for some help with constructing a form a has_many :through relationship.

I have three classes:

order.rb

has_many :selections
has_many :items, :through =>:selections;

item.rb

has_many :selections
has_many :orders, :through => :selections

selection.rb

belongs_to :order
belongs_to :item

Selections has an extra property on it to capture the quantity of the selected item.

What I'm trying to do is create a form that allows a user check a box indicating the type of item they want and supply a quantity.

_X_ Food1 _6_
___ Food2 __
_X_ Food3 _1_

My problem is that I don't have any idea how to name the form elements so that they make it into the controller. I can't use

<%= collection_check_boxes(:order, :item_ids, @course.items.all, :id, :name, {:item_wrapper_class => 'checkbox_container'}) %>

because that will just print the food with checkboxes and I need to capture the quantity for each selected item.

What I could use help with is how to create the _form.html.erb as well as special gotcha's that I might need to have in my controller.

Kip Diskin
  • 192
  • 1
  • 6

1 Answers1

0

First off, item.rb should read,

has_many :selections
has_many :orders, :through => :selections

Secondly, why are you using a check box? Why not just forms that if empty default to zero or nil? That is, if you have more than one, you can use it as a truthy value in lieu of a check being checked or not.

tsiege
  • 468
  • 1
  • 4
  • 17
  • Thanks for catching the typo on the item. Your suggesting that I just do Food1 [2] Food2 [ ] Food3 [4] Thats definitely a better way but I'm still unsure how to construct the form so that the selections get correctly populated by the controller. – Kip Diskin May 04 '14 at 02:37
  • Can you go a little further into what you're trying to do? – tsiege May 04 '14 at 14:53