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.

exists? should not instantiate a record

#5897

See here:

http://github.com/rails/rails/blob/master/activerecord/lib/active_r...

This causes the following line to raise errors:

Parent.children.named_scope.exists?(:published => true)

Because it will try to preload the association and since exists? only selects the primary key, it will fail saying that a association key column does not exist.

It would also be an obvious performance improvement.

Reported by José Valim · October 30th, 2010 @ 08:53 PM

State: stale
Milestone: 3.x
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Jeff Kreeftmeijer
  2. Cesario
    Cesario

    @Jose: i wanted to work on that but I can't reproduce it with

    def test_exists_with_scopes
      assert_nothing_raised do
        authors(:david).posts.containing_the_letter_a.exists?(:title => 'Welcome to the weblog')
      end
    end
    

    with current Rails' master. This test just passes.

    Am I testing it right or is this bug solved?

    November 12th, 2010 @ 05:55 PM

  3. José Valim
    José Valim

    It seems your test is right. Is Author doing has_many :posts and Post doing belongs_to :author or they are passing additional arguments? Have you checked if that piece of the code I mentioned above is being called?

    Thanks for looking into this!

    November 12th, 2010 @ 05:59 PM

  4. Cesario
    Cesario

    There's an extension on the belong_to :author association but nothing more than that.
    Yes the piece of code you mention is being called.
    About the published attribute you put in the code, are you sure it is stored at the children level?

    November 12th, 2010 @ 06:19 PM

  5. José Valim
    José Valim

    Yes, it is at the child level. But I just checked again and found out that my "comments" has a include clause, like this:

    class Post
      has_many :comments, :include => :comment_author
    end
    

    So it is quite likely that it is this other include that is making it fail?

    November 12th, 2010 @ 06:41 PM

  6. Cesario
    Cesario

    No luck in trying to reproduce that one unfortunately. :/

    November 12th, 2010 @ 09:23 PM

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

    February 26th, 2011 @ 12:00 AM

  8. rails
    rails
    • State changed from open to stale

    February 26th, 2011 @ 12:00 AM