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.

default_scope breaks (update|delete|destroy)_all in some cases

#5817

Rails 3.

The code posted below, is purely for illustration of this problem. Hopefully this does not confuse the issue.

Given I have a Post model, and a Comment model. Post has_many Comments, and Comment belongs_to Post.

With a default_scope set on the Post model, defining joins() and where() relations. In this case where() is dependent on joins().

Normally Posts wouldn't be dependent on Comments. Again, I just want to give a simple example. This could be any case when where() is dependent on joins() or other methods.

    class Post < ActiveRecord::Base
      has_many :comments, :dependent => :destroy
      
      default_scope joins(:comments).where("comments.id < 999")
    end

    class Comment < ActiveRecord::Base
      belongs_to :post, :counter_cache => true
    end

Running the following command:

    Post.update_all(:title => Time.now)

Produces the following query, and ultimately throws ActiveRecord::StatementInvalid:

    UPDATE `posts` SET `title` = '2010-10-15 15:59:27'  WHERE (comments.id < 999)

Again, update_all, delete_all, destroy_all behave the same way. I discovered this behaviour when my application complained when trying to update the counter_cache. Which eventually drills down into update_all.

Reported by releod · October 15th, 2010 @ 05:13 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 19th, 2010 @ 07:08 AM

  2. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 21st, 2010 @ 03:37 AM

  3. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 29th, 2011 @ 01:00 AM

  4. rails
    rails
    • State changed from open to stale

    March 29th, 2011 @ 01:00 AM