I have 2 models, Category and LineItemTypes
There are already plenty existing of both, it is now a requirement for
them to be associated. Being many LineItemTypes for every Category.
I have added accepts_nested_attributes_for :line_item_types on Category
I’ve tried using a hidden_field on a form to create a list of existing
associated LineItemTypes:
-
form_for @category do |form|
%ul#categorised- form.fields_for :line_item_types do |line_item_types|
-categorised.each do |l|
%li
=l.description
=line_item_types.hidden_field :category_id
=form.submit
- form.fields_for :line_item_types do |line_item_types|
If I add an item to that list, I get errors saying that a LineItemType
for that Category can’t be found. I thought
accepts_nested_attributes_for would add the association if it doesn’t
exist. Or is it only for ‘creating’ new records and modifying existing
relationships, not creating new relationships.
a.update_attributes({:line_item_types_attributes => [{:id => 2767}, {:id
=> LineItemType.find(2).id}]})
ActiveRecord::RecordNotFound: Couldn’t find LineItemType with ID=2 for
Category with ID=1
Any ideas without having to write something to traverse the resulting
form params and create the associations? Or an even easier way to
achieving this? I have an existing association with