HABTM relationships and retrieving multiple selects in contr


#1

Hello all,

I have a product table and an option table that are connected to one
another
via a join table. These two tables both contain has_and_belong_to_many
relationships with each other.

I am trying to setup a page where I will create a product and add in its
options. I have already populated the options table with data to draw
from.

so, to start, here is the method from my controller:
def new
@product = Product.new()
@opt = Option.find_all
end

and we go off to the “new” view, where I have this:

<select name=“product[options][]”, size=‘5’, multiple='multiple>
<%= options_from_collection_for_select(@opt, “id”, “name”, @
product.options) %>

other product creation code goes here…

This select statement does correctly draw the options that were found by
the
controller and put into @opt. before displaying this page.

This form submits to the create method of the controller, where I have
this:
def create
@product = Product.new(params[:product])
@product.save
end

Well, this create controller does not work. WebBrick reports “option
expected, got string”. I see that the option ID I select is being
passed in
as a parameter like “options”=>[“3”]. But obviously I cannot retrieve
the
option ids, create the options, and add them to the product, and save it
all
in such a simplistic manner. I’ve tried to figure out how to, but
can’t.
I’ve read the Agile book, on page 356, but still can’t understand.

Can anyone provide some help?

Thanks,
Shelby