{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/7256","labels_url":"https://api.github.com/repos/rails/rails/issues/7256/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/7256/comments","events_url":"https://api.github.com/repos/rails/rails/issues/7256/events","html_url":"https://github.com/rails/rails/issues/7256","id":6024587,"number":7256,"title":"AcitveRecord won't accepts_nested_attributes_for new associated records with nested existing records","user":{"login":"wulftone","id":142784,"avatar_url":"https://avatars.githubusercontent.com/u/142784?v=3","gravatar_id":"","url":"https://api.github.com/users/wulftone","html_url":"https://github.com/wulftone","followers_url":"https://api.github.com/users/wulftone/followers","following_url":"https://api.github.com/users/wulftone/following{/other_user}","gists_url":"https://api.github.com/users/wulftone/gists{/gist_id}","starred_url":"https://api.github.com/users/wulftone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wulftone/subscriptions","organizations_url":"https://api.github.com/users/wulftone/orgs","repos_url":"https://api.github.com/users/wulftone/repos","events_url":"https://api.github.com/users/wulftone/events{/privacy}","received_events_url":"https://api.github.com/users/wulftone/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":17,"created_at":"2012-08-03T21:37:00Z","updated_at":"2015-04-17T18:40:14Z","closed_at":"2012-09-04T19:57:29Z","body":"ActiveRecord doesn't seem to understand that, given a set of params for an existing record with nested attributes, it can create a new nested record that itself has a nested _existing_ record.  (Relations tree: `Existing -> New -> Existing`)\n\nIs this a bug, or am I missing something?\n\nLet me show you a simple example.\n\nHere are my models:\n\n    class User < ActiveRecord::Base\n      has_many :posts\n      attr_accessible :name, :posts_attributes\n      accepts_nested_attributes_for :posts\n    end\n\n    class Post < ActiveRecord::Base\n      belongs_to :group\n      belongs_to :user\n      attr_accessible :content, :title, :group_attributes\n      accepts_nested_attributes_for :group\n    end\n\n    class Group < ActiveRecord::Base\n      has_many :posts\n      attr_accessible :name\n    end\n\nI've made one record in each table, and related them accordingly, so each table has a record in it with an `id=1`--this is known.  Now, if I have an existing User, a new Post, and an existing Group, and try to update that record using `accepts_nested_attributes_for`, it doesn't like it:\n\n    1.9.3-p125 :044 > params\n    {\n                      :id => 1,\n                    :name => \"Billy\",\n        :posts_attributes => [\n            [0] {\n                              :title => \"Title\",\n                            :content => \"Some magnificent content for you!\",\n                :group_attributes => {\n                      :id => 1,\n                    :name => \"Group 1\"\n                }\n            }\n        ]\n    }\n    1.9.3-p125 :045 > u\n    #<User:0x00000002f7f380> {\n                :id => 1,\n              :name => \"Billy\",\n        :created_at => Fri, 03 Aug 2012 20:21:37 UTC +00:00,\n        :updated_at => Fri, 03 Aug 2012 20:21:37 UTC +00:00\n    }\n    1.9.3-p125 :046 > u.update_attributes params\n       (0.1ms)  begin transaction\n       (0.1ms)  rollback transaction\n    ActiveRecord::RecordNotFound: Couldn't find Group with ID=1 for Post with ID=\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:462:in `raise_nested_attributes_record_not_found'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:332:in `assign_nested_attributes_for_one_to_one_association'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:288:in `group_attributes='\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:94:in `block in assign_attributes'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:93:in `each'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:93:in `assign_attributes'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/base.rb:498:in `initialize'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/reflection.rb:183:in `new'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/reflection.rb:183:in `build_association'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/associations/association.rb:233:in `build_record'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/associations/collection_association.rb:112:in `build'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:405:in `block in assign_nested_attributes_for_collection_association'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:400:in `each'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:400:in `assign_nested_attributes_for_collection_association'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:288:in `posts_attributes='\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:78:in `each'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/persistence.rb:216:in `block in update_attributes'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/transactions.rb:208:in `transaction'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/persistence.rb:215:in `update_attributes'\n      from (irb):15\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.7/lib/rails/commands/console.rb:47:in `start'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.7/lib/rails/commands/console.rb:8:in `start'\n      from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.7/lib/rails/commands.rb:41:in `<top (required)>'\n      from script/rails:6:in `require'\n      from script/rails:6:in `<main>'1.9.3-p125 :047 > \n\nIt thinks it can't find a group (with a known ID), related to a new Post.\nIt works when I remove the ID from the `group_attributes` (but it creates a new group record).\nIt works when I give the `posts_attributes` an ID, and remove the id from the `group_attributes` (and again creates a new group).\nIt also works when they all have IDs.\n\n  The relationship is working:\n\n    1.9.3-p125 :059 > p = Post.new( { group_attributes: { name: 'Testing' } } )\n    #<Post:0x00000004212380> {\n                :id => nil,\n             :title => nil,\n           :content => nil,\n          :group_id => nil,\n           :user_id => nil,\n        :created_at => nil,\n        :updated_at => nil\n    }\n    1.9.3-p125 :060 > p.group\n    [\n        [0] #<Group:0x00000004211868> {\n                    :id => nil,\n                  :name => \"Testing\",\n            :created_at => nil,\n            :updated_at => nil\n        }\n    ]\n\nIt also completely works when using `posts_attributes` and `group_attributes` during `User` creation, if all of the records are new.\n\nShouldn't it work still in the first example?  ActiveRecord should be smart enough to figure this out...!"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/94048728","html_url":"https://github.com/rails/rails/issues/7256#issuecomment-94048728","issue_url":"https://api.github.com/repos/rails/rails/issues/7256","id":94048728,"user":{"login":"finbarr","id":211517,"avatar_url":"https://avatars.githubusercontent.com/u/211517?v=3","gravatar_id":"","url":"https://api.github.com/users/finbarr","html_url":"https://github.com/finbarr","followers_url":"https://api.github.com/users/finbarr/followers","following_url":"https://api.github.com/users/finbarr/following{/other_user}","gists_url":"https://api.github.com/users/finbarr/gists{/gist_id}","starred_url":"https://api.github.com/users/finbarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/finbarr/subscriptions","organizations_url":"https://api.github.com/users/finbarr/orgs","repos_url":"https://api.github.com/users/finbarr/repos","events_url":"https://api.github.com/users/finbarr/events{/privacy}","received_events_url":"https://api.github.com/users/finbarr/received_events","type":"User","site_admin":false},"created_at":"2015-04-17T18:40:14Z","updated_at":"2015-04-17T18:40:14Z","body":"@grillermo I don't unfortunately. I'm constructing the json payload by hand and sending it to the server via ajax."}}