This project is archived and is in readonly mode.

#3175 ✓wontfix
Lars Klevan

named_scope.is_a? loads association

Reported by Lars Klevan | September 9th, 2009 @ 04:14 PM

I've run into this issue twice where I have different behavior when I'm dealing with an Array versus a named scope and checking is_a? is unexpectedly loading a large association.

messages = current_user.messages
messages = [{:text => 'Welcome'}] if messages.empty?
...
messages.is_a?(Array) ? messages : messages.find(:all, :limit => 10)

There are other ways to do this that will not load the association but loading all the records in the association is certainly unexpected when you look at this code.

Comments and changes to this ticket

  • Ryan Bigg

    Ryan Bigg June 6th, 2010 @ 02:45 AM

    Why are you doing it like this? Why not limit the original call to 10 messages and then if it's empty build a message.

    messages = current_user.messages.all(:limit => 10)
    messages = [current_user.messages.build(:text => 'Welcome')] if messages.empty?
    
  • Ryan Bigg

    Ryan Bigg June 9th, 2010 @ 12:30 PM

    • State changed from “new” to “wontfix”

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages