This project is archived and is in readonly mode.
accepts_nested_attributes, validates_presence_of - saves associated model
Reported by qoobaa | October 8th, 2009 @ 03:32 PM | in 2.3.10
We've got two model classes:
class User < ActiveRecord::Base
has_many :posts
after_save { puts "after_save" }
end
class Post < ActiveRecord::Base
belongs_to :user
validates_presence_of :user
accepts_nested_attributes_for :user
end
>> User.create!
after_save
>> Post.create!(:user => User.first)
after_save
=> #<Post id: 1, user_id: 1, created_at: "2009-10-08 14:02:04", updated_at: "2009-10-08 14:02:04">
>> Post.first.touch
after_save
=> true
We're executing after_save callback by touching associated post model. It's totally unexpected behavior. It's quite similar to #2578, but the patch doesn't solve the problem.
Comments and changes to this ticket
-
Eloy Duran December 30th, 2009 @ 07:54 PM
- Milestone set to 2.3.6
- Assigned user set to Eloy Duran
-
Eloy Duran July 8th, 2010 @ 08:39 AM
- Assigned user changed from Eloy Duran to José Valim
- Importance changed from to
-
Subba July 14th, 2010 @ 10:08 AM
I found the fix for this
below is the reason why children save if parent saves(even when they are not changed)
activerecord/lib/active_record/autosave_association.rb at line 375saved = association.save(:validate => !autosave) if association.new_record? || autosave
i am providing patch for this.
this is currently happening in rails3 and rails23stable.
so i am providing two different patches. -
Repository July 18th, 2010 @ 10:36 AM
- State changed from new to resolved
(from [4a0d7c1a439c6ad8d35bf514761824e51fa07df2]) save on parent should not cascade to child unless child changed [#3353 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/4a0d7c1a439c6ad8d35bf514761824... -
José Valim July 18th, 2010 @ 10:37 AM
- State changed from resolved to open
Patch no longer applied for 2-3-stable. Could you please rebase? Thanks!
-
Subba July 21st, 2010 @ 11:16 AM
Jose due to the above patch me below case is not working.
changes to grand children no longer works if parent is not changed.
as saving children is one of the callback.
would you rollback above patch.
is there anyway to detect changes to children@ship = Ship.create(:name => 'Nights Dirty Lightning') @pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?") @parrot = @pirate.parrots.create!(:name => 'Posideons Killer') @parrot.name = "NewName" @ship.save assert_equal 'NewName', @parrot.reload.name
-
Repository July 21st, 2010 @ 01:52 PM
(from [01add55d6a6a67fb487afa040998575111511b6d]) Revert "save on parent should not cascade to child unless child changed [#3353 state:open]"
Please check Lighthouse for more information.
This reverts commit 4a0d7c1a439c6ad8d35bf514761824e51fa07df2.
http://github.com/rails/rails/commit/01add55d6a6a67fb487afa04099857... -
Repository December 13th, 2010 @ 04:14 PM
- State changed from open to committed
(from [658bb4fa25db0b3f61bfb64028274f2365cad506]) Only call save on belongs_to associations if the record has changed or any nested associations have changed (resolves #3353)
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/658bb4fa25db0b3f61bfb64028274... -
Repository December 15th, 2010 @ 08:40 PM
(from [bda16eb0920780509467bcf051d1a01639357903]) Only call save on belongs_to associations if the record has changed or any nested associations have changed (resolves #3353)
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/bda16eb0920780509467bcf051d1a...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 3353 accepts_nested_attributes, validates_presence_of - saves associated model (from [4a0d7c1a439c6ad8d35bf514761824e51fa07df2]) save on...
- 3353 accepts_nested_attributes, validates_presence_of - saves associated model (from [01add55d6a6a67fb487afa040998575111511b6d]) Revert ...
- 3353 accepts_nested_attributes, validates_presence_of - saves associated model (from [658bb4fa25db0b3f61bfb64028274f2365cad506]) Only ca...
- 3353 accepts_nested_attributes, validates_presence_of - saves associated model (from [bda16eb0920780509467bcf051d1a01639357903]) Only ca...