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.

small enhancement to query scopes

#5960

If you do this (which is an easy mistake for a newbie like me to make):

module RailsAdmin
class History < ActiveRecord::Base

scope :most_recent, lambda {|table| where("tbl = ?", table).order("updated_at").last }

...

you get this crash:

ruby-1.8.7-p302 > RailsAdmin::History.most_recent("Team")
NoMethodError: undefined method includes_values' for #<RailsAdmin::History:0xb6a82d78><br/>

from {snip}/activemodel-3.0.1/lib/active_model/attribute_methods.rb:364:in `method_missing'
from {snip}/activerecord-3.0.1/lib/active_record/attribute_methods.rb:46:in `method_missing'
from {snip}/activerecord-3.0.1/lib/active_record/attribute_methods.rb:44:in `send'
from {snip}/activerecord-3.0.1/lib/active_record/attribute_methods.rb:44:in `method_missing'
from {snip}/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:10:in `send'
from {snip}/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:10:in `merge'
from {snip}/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:9:in `each'
from {snip}/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:9:in `merge'
from {snip}/activerecord-3.0.1/lib/active_record/named_scope.rb:112:in `most_recent'
from (irb):3

This happens because spawn_methods.merge() is trying to merge a Relation with an ActiveRecord model object (the result of the last() method at the end of the scope).

Reported by toby cabot · November 12th, 2010 @ 01:54 PM

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

Activity

  1. toby cabot
    toby cabot
    • Tag set to patch

    Here's a small patch that allows this behavior. It adds a special case to the scope processing (there were already 3 cases so this isn't a big change) that avoids the merge when the result of the scope is a single AR model object.

    November 12th, 2010 @ 01:58 PM

  2. toby cabot
    toby cabot
    • Tag changed from patch to activerecord rails3, patch

    November 15th, 2010 @ 08:15 PM

  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.

    February 16th, 2011 @ 12:00 AM

  4. rails
    rails
    • State changed from open to stale

    February 16th, 2011 @ 12:00 AM

  5. Jeff Gardner
    Jeff Gardner

    I'm still having this issue with rails 3.0.5. There is a patch above, can that be pulled into the main codebase?

    March 8th, 2011 @ 01:09 PM

  6. toby cabot
    toby cabot
    • Tag cleared.
    • State changed from stale to open

    [state:open]

    This is a small but, I think, worthwhile change to make Rails scopes a little more forgiving to new users.

    I've rebased the patch (5960-2.diff) so it applies cleanly to 7bd70dc.

    March 15th, 2011 @ 09:37 PM

  7. greg.silcox (at cumulosity)
    greg.silcox (at cumulosity)

    I'm also having this problem in rails 3.0.5. My scope looks a lot like the example given above:

    class StudentSession < ActiveRecord::Base
    scope :last_updated, lambda { |user_id, group_id|

    where( :student_id => user_id, :group_id => group_id ).
    StudentSession.order( :updated_at ).first }
    

    end

    It seems to only happen when I have a single record that matches.

    March 18th, 2011 @ 03:35 PM