Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

accepts_nested_attributes, validates_presence_of - saves associated model

#3353

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.

Reported by qoobaa · October 8th, 2009 @ 03:32 PM

State: committed
Milestone: 2.3.10
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Eloy Duran
    Eloy Duran
    • Milestone set to 2.3.6
    • Assigned user set to Eloy Duran

    December 30th, 2009 @ 07:54 PM

  2. Rizwan Reza
  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.6 to 2.3.7

    May 23rd, 2010 @ 05:54 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.7 to 2.3.8

    May 24th, 2010 @ 09:40 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.8 to 2.3.9

    May 25th, 2010 @ 11:45 PM

  6. Eloy Duran
    Eloy Duran
    • Assigned user changed from Eloy Duran to José Valim
    • Importance changed from to

    July 8th, 2010 @ 08:39 AM

  7. Subba
    Subba

    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 375

      saved = 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.

    July 14th, 2010 @ 10:08 AM

  8. Repository
    Repository
    • 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...

    July 18th, 2010 @ 10:36 AM

  9. José Valim
    José Valim
    • State changed from resolved to open

    Patch no longer applied for 2-3-stable. Could you please rebase? Thanks!

    July 18th, 2010 @ 10:37 AM

  10. Subba
    Subba

    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
    

    July 21st, 2010 @ 11:16 AM

  11. José Valim
    José Valim

    Do you want me to revert the patch?

    July 21st, 2010 @ 11:24 AM

  12. Subba
  13. Repository
    Repository

    (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...

    July 21st, 2010 @ 01:52 PM

  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  15. Repository
    Repository
    • 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...

    December 13th, 2010 @ 04:14 PM

  16. Repository
    Repository

    (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...

    December 15th, 2010 @ 08:40 PM