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.

[AutosaveAssociation] should not validate associated model if marked_for_destruction

#2064

While coding an application based on 2.3RC1 I've stuck with this behaviour:


  def test_should_skip_validation_and_destroy_the_associated_model
    @pirate.ship.update_attribute(:name, '')

    assert !@pirate.valid?
    assert @pirate.reload.valid?

    assert_difference('Ship.count', -1) do
      @pirate.update_attributes({ :ship_attributes => { :id => @ship.id,  :_delete => true } })
    end
    assert @pirate.valid?
  end
end

Should not validate associated model if it were marked_for_destruction.

Reported by Dmitry Polushkin · February 24th, 2009 @ 11:33 PM

State: resolved
Milestone: 2.3.6
Assigned to: Eloy Duran Eloy Duran
Importance: Low

Activity

  1. Dmitry Polushkin
    Dmitry Polushkin
    • Tag changed from 2.3-rc1, accepts_nested_attributes_for, validation to 2.3-rc1, accepts_nested_attributes_for, arked_for_destruction, validation
    • Title changed from nested attributes to [nested attributes] should not validate associated model marked_for_destruction

    February 24th, 2009 @ 11:35 PM

  2. Eloy Duran
    Eloy Duran
    • Title changed from [nested attributes] should not validate associated model marked_for_destruction to [AutosaveAssociation] should not validate associated model if marked_for_destruction

    Good catch!

    February 24th, 2009 @ 11:56 PM

  3. DHH
    DHH
    • Milestone cleared.

    February 25th, 2009 @ 06:24 PM

  4. Eloy Duran
  5. Eloy Duran
  6. Eloy Duran
    Eloy Duran
    • Assigned user changed from Eloy Duran to Michael Koziarski

    February 26th, 2009 @ 12:47 PM

  7. Dmitry Polushkin
    Dmitry Polushkin

    Just applied both patches, works great! Thanks.

    February 27th, 2009 @ 12:16 AM

  8. Repository
    Repository

    (from [e2b925fa68203607fb4f9a9aa457c0435c969f9a]) Don't validate records of an :autosave association if they're marked for destruction. [#2064 [AutosaveAssociation] should not validate associated model if marked_for_dest... status:resolved]

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...

    February 27th, 2009 @ 12:57 PM

  9. DHH
    DHH
    • State changed from new to resolved

    February 27th, 2009 @ 12:58 PM

  10. Dmitry Polushkin
    Dmitry Polushkin
    • Assigned user changed from Michael Koziarski to Eloy Duran

    Oh, found another bug related to this topic. When Model marked_for_destruction? and has custom validation, then it's enters to the validation method, but it shouldn't:

    
      class ::Ship
        validate :validate_name
    
        private
    
        def validate_name
         if name && 'mustnt-go-here' == name
           errors.add(:name, :name_invalid)
           throw :mustnt_validate_when_marked_for_destruction
         end
        end
      end
    
      def test_should_skip_validation_and_destroy_the_associated_model
        @pirate.ship.update_attribute(:name, 'mustnt-go-here')
        @pirate.ship.mark_for_destruction
    
        assert_nothing_thrown do
          @pirate.update_attributes({ :ship_attributes => { :id => @ship.id,  :_delete => true } })
        end
      end
    

    Thanks!

    March 23rd, 2009 @ 02:23 AM

  11. Eloy Duran
    Eloy Duran
    • Tag changed from 2.3-rc1, accepts_nested_attributes_for, arked_for_destruction, validation to 2.3.2, accepts_nested_attributes_for, marked_for_destruction, validation
    • State changed from resolved to open

    @Dmitry: Sorry that I missed your new comment. Next time please create a new ticket or change the state to open again.

    About the problem; have you investigated already where the problem lies? And/or maybe a naive patch or something?

    April 22nd, 2009 @ 09:31 AM

  12. Dmitry Polushkin
    Dmitry Polushkin

    @Eloy: no, I've tried, but didn't found where is the problem lies.

    April 22nd, 2009 @ 09:48 AM

  13. Dmitry Polushkin
    Dmitry Polushkin

    The problem is in:

    
        def association_valid?(reflection, association)
          unless valid = association.valid?
            if reflection.options[:autosave]
              unless association.marked_for_destruction?
    

    condition unless association.marked_for_destruction? must be before the valid? method invoked.

    April 23rd, 2009 @ 03:37 PM

  14. Eloy Duran
    Eloy Duran

    That seems obvious yes, good catch again! Are you gonna try to write a patch for it? If not, that's ok then I'll fix it later on.

    April 23rd, 2009 @ 03:43 PM

  15. Dmitry Polushkin
    Dmitry Polushkin

    Not so good in git, actually. :)

    April 23rd, 2009 @ 03:54 PM

  16. Dmitry Polushkin
  17. Michael Koziarski
  18. Dmitry Polushkin
    Dmitry Polushkin

    New patch is here. Please, if it is possible, push it before 2.3.4 will come out. Thanks.

    July 3rd, 2009 @ 02:49 PM

  19. Dmitry Polushkin
  20. José Valim
    José Valim

    +1, applied and it works as expected.

    July 12th, 2009 @ 12:22 PM

  21. Eloy Duran
    Eloy Duran
    • State changed from open to verified

    I have verified and applied this patch on my branch of 2-3-stable, which Micheal will look at and merge in before 2.3.4.

    http://github.com/alloy/rails/commit/dbc56d67b644944dcff09afbcb2f7d...

    Thanks!

    July 12th, 2009 @ 12:35 PM

  22. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  23. Repository
  24. Repository
  25. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:01 PM

  26. bingbing