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.

named_scope.is_a? loads association

#3175

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.

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

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

Activity

  1. Ryan Bigg
    Ryan Bigg

    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?
    

    June 6th, 2010 @ 02:45 AM

  2. Ryan Bigg
    Ryan Bigg
    • State changed from new to wontfix

    June 9th, 2010 @ 12:30 PM