This project is archived and is in readonly mode.

#5960 open
toby cabot

small enhancement to query scopes

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

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

Comments and changes to this ticket

  • toby cabot

    toby cabot November 12th, 2010 @ 01:58 PM

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

  • toby cabot

    toby cabot November 15th, 2010 @ 08:15 PM

    • Tag changed from patch to activerecord rails3, patch
  • rails

    rails February 16th, 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 16th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”
  • Jeff Gardner

    Jeff Gardner March 8th, 2011 @ 01:09 PM

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

  • toby cabot

    toby cabot March 15th, 2011 @ 09:37 PM

    • Tag cleared.
    • State changed from “stale” to “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.

  • greg.silcox (at cumulosity)

    greg.silcox (at cumulosity) March 18th, 2011 @ 03:35 PM

    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 }


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

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>

People watching this ticket