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.

Touch called on parent destroy

#3461

I have two models: Project and Tags

class Project < ActiveRecord::Base
  has_many :tags, :dependent => :destroy
end

class Tag < ActiveRecord::Base
  belongs_to :project, :touch => true
end

And every project must have at least one tag (I skipped the valiations). So when project.destroy is called, I get the following error message:

...application/vendor/rails/activerecord/lib/active_record/validations.rb:1090:in save_without_dirty!' ...application/vendor/rails/activerecord/lib/active_record/dirty.rb:87:insave_without_transactions!' ...application/vendor/rails/activerecord/lib/active_record/transactions.rb:200:in save!' ...application/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:136:intransaction' ...application/vendor/rails/activerecord/lib/active_record/transactions.rb:182:in transaction' ...application/vendor/rails/activerecord/lib/active_record/transactions.rb:200:insave!' ...application/vendor/rails/activerecord/lib/active_record/transactions.rb:208:in rollback_active_record_state!' ...application/vendor/rails/activerecord/lib/active_record/transactions.rb:200:insave!' ...application/vendor/rails/activerecord/lib/active_record/timestamp.rb:37:in touch' ...application/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:221:insend' ...application/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:221:in method_missing' ...application/vendor/rails/activerecord/lib/active_record/associations.rb:1380:inbelongs_to_touch_after_save_or_destroy_for_project' ...application/vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in send' ...application/vendor/rails/activesupport/lib/active_support/callbacks.rb:178:inevaluate_method' ...application/vendor/rails/activesupport/lib/active_support/callbacks.rb:166:in call' ...application/vendor/rails/activesupport/lib/active_support/callbacks.rb:93:inrun' ...application/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in each' ...application/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:insend'

It seems that every tag.destroy call project.touch, and my guess the last touch is invalid because the project needs at least one tag. In my opinion, when the project is destroyed it shouldn't be touched at all.

Does someone know a workaround? Thanks

Reported by Helmut Juskewycz · November 5th, 2009 @ 09:46 AM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. joel
    joel
    • Importance changed from to

    Why has this not been addressed? This is affecting my application also, you should be able to limit :touch to save, update, or destroy. Otherwise validations are running on an object that is trying to be destroyed (the parent in the dependent destroy).

    August 3rd, 2010 @ 05:34 PM

  2. Neeraj Singh
    Neeraj Singh

    @Joel what version of rails you are using?

    August 3rd, 2010 @ 06:54 PM

  3. perrycz
    perrycz
    • Tag changed from 2.3.4, :dependent, :destroy, active_record, destroy, touch to 2.3.4, 2.3.8, 2.3.9, :dependent, :destroy, active_record, destroy, touch

    Same here. Tested on Rails 2.3.8 and 2.3.9.

    September 15th, 2010 @ 01:03 AM

  4. Tony Daly
    Tony Daly

    Any news on this? Also experiencing this in 2.3.8.

    January 24th, 2011 @ 06:25 PM

  5. Ryan Wallace
    Ryan Wallace

    FYI: This is fixed in Rails 3

    March 16th, 2011 @ 10:39 PM