HABTM Associations In View

Hello,
I wish I had the time to figure this out but I’m on a limited timeline
to convince people to look at RoR instead of Drupal, so I need to get
something relatively simple working fast. Any help or pointers would
be appreciated!

I have two HABTM many models, recipe_item and purchase_item. I need a
form to designate in my view that any number of purchase_items or
recipe_items can be a component of a given recipe_item. It looks like
Ryan B.’ screencast on nested model forms provides a lot of help on
a similar issue, but I’m having trouble picking apart the logic for my
issue. Regardless at the moment as a test I’m just trying to get a
single select on the recipe_item view to submit the purchase_item
association. When I submit I get this error:

PurchaseItem(#20112400) expected, got Array(#102050)

Here is my code:

Models:

class PurchaseItem < ActiveRecord::Base
has_and_belongs_to_many :recipe_items
end

class RecipeItem < ActiveRecord::Base
has_and_belongs_to_many :purchase_items
end

Controllers are left completely as generated by 2.3 Rails scaffolding.

New recipe_item View:

New recipe_item

<% form_for(@recipe_item) do |f| %>
<%= f.error_messages %>

<%= f.label :name %>
<%= f.text_field :name %>

<% f.fields_for :purchase_items do |n| %> <%= n.label :purchase_item_id %> <%= n.select :purchase_item_id, PurchaseItem.all.collect {|a| [a.name, a.id] }%> <% end %>

<%= f.submit 'Create' %>

<% end %>

<%= link_to ‘Back’, recipe_items_path %>

I’m aware this can be DRYed up but like I said right now I just need
to demonstrate something working for my team. Any help would be
appreciated!

Thanks,
Andy

try this.

<%= n.select :purchase_item_id, PurchaseItem.all.collect {|a| [a.name,
a.id] }, @recipe.purchase_item.id %>

Havent tested it so it may or may not work.