This project is archived and is in readonly mode.

#5897 ✓stale
José Valim

exists? should not instantiate a record

Reported by José Valim | October 30th, 2010 @ 08:53 PM | in 3.x

See here:

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.

Comments and changes to this ticket

  • Jeff Kreeftmeijer
  • Cesario

    Cesario November 12th, 2010 @ 05:55 PM

    @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')

    with current Rails' master. This test just passes.

    Am I testing it right or is this bug solved?

  • José Valim

    José Valim November 12th, 2010 @ 05:59 PM

    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!

  • Cesario

    Cesario November 12th, 2010 @ 06:19 PM

    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?

  • José Valim

    José Valim November 12th, 2010 @ 06:41 PM

    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

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

  • Cesario

    Cesario November 12th, 2010 @ 09:23 PM

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

  • rails

    rails February 26th, 2011 @ 12:00 AM

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

  • rails

    rails February 26th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>