This project is archived and is in readonly mode.
assign_nested_attributes_for_collection_association ought to assign object's for collection if an id is given but the object is currently not associated
-
George Montana Harkin
- Tag set to accepts_nested_attributes_for, belongs_to
I ran into this issue with 2.3.8. Currently it throws a nested_attributes_record_not_found error on line 288 of nested_attributes.rb.
This might have already been fixed somewhere else.
The use case which is causing our issue is we have Model 1 which is being created a new. Model 2 already exists and needs to be associated with Model 1 on create. Since the model_2_attributes params contains an id it throws this error.
-
Subba
Hi George
is below code explains your problemclass Ship < ActiveRecord::Base has_one :pirate, :inverse_of => :ship accepts_nested_attributes_for :pirate, :allow_destroy => true end
class Pirate < ActiveRecord::Base belongs_to :ship, :inverse_of => :pirate end
pirate = Pirate.create(:name => 'Johnny')
ship = Ship.new ship.pirate_attributes = { :id => pirate.id }
ActiveRecord::RecordNotFound: Couldn't find Pirate with ID=2 for Ship with ID=If so the correct way to assign attribute is
ship.pirate_attributes = { :pirate_id => pirate.id }I wrote this block to create test case for your problem. please apologize me if i misunderstood you.
-
George Montana Harkin
thanks subbarao, that looks simliar to the issue we're running into.
That fix is kind of a pain as we're using fields_for which automatically inserts an :id field into the attributes rather than an :pirate_id attribute.
so our example is:
class Ship < ActiveRecord::Base
has_one :pirate accepts_nested_attributes_for :pirate, :allow_destroy => true endclass Pirate < ActiveRecord::Base
belongs_to :ship endparams:
{ :ship => { :name => 'Titanic', :pirate_attributes => { :id => '2', :name => 'Leeroy' }}}ship = Ship.new(params[:ship])
ActiveRecord::RecordNotFound: Couldn't find Pirate with ID=2 for Ship with ID=
Should our fix be a patch to fields_for when dealing with a belongs_to relationship to use :pirate_id instead of :id? Will this even work?
Thanks!
-
Subba
it will fix your problem. nested attributes hash if contains id it believes existing record.
-
George Montana Harkin
We shouldn't have to named the id something specific. :id is the name of the field and it should create or assign the related Pirate automatically from that. If it doesn't then there is no point to the rails field name conventions.
This is definitely a bug. It is now having issues with one to many. There must be other tickets with the same issue. Seems like it is 2.3.8 related.
-
George Montana Harkin
Yeah, this is a duplicate of #2415 accepts_nested_attributes_for does't work when nested_attributes hash has an ...
-
Santiago Pastorino
- State changed from new to open
- Importance changed from to
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino
- State changed from open to stale