Ruby 当来自控制台的同一查询工作时,作用域查询失败

Ruby 当来自控制台的同一查询工作时,作用域查询失败,ruby,activerecord,ruby-on-rails-3,Ruby,Activerecord,Ruby On Rails 3,当查询直接工作时,为什么使用范围查询会失败 我的模型: class Score < ActiveRecord::Base belongs_to :query belongs_to :pull#, :counter_cache => true scope :latest, find_by_sql("SELECT * FROM (SELECT * FROM scores ORDER BY created_at DESC) s GROUP BY query_id") end

当查询直接工作时,为什么使用范围查询会失败

我的模型:

class Score < ActiveRecord::Base
  belongs_to :query
  belongs_to :pull#, :counter_cache => true

  scope :latest, find_by_sql("SELECT * FROM (SELECT * FROM scores ORDER BY created_at DESC) s  GROUP BY query_id")
end
启动控制台

localhost:~/code/myproject[master]% rails c

Loading development environment (Rails 3.0.1)
>> Score.latest
NoMethodError: undefined method `includes_values' for #<Array:0x7f28f2cb0e18>
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:10:in `send'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:10:in `merge'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:9:in `each'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/spawn_methods.rb:9:in `merge'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/named_scope.rb:112:in `latest'
        from (irb):1
但是直接使用相同的查询不会有任何问题

>> Score.find_by_sql("SELECT * FROM (SELECT * FROM scores ORDER BY created_at DESC) s  GROUP BY query_id")
=> [#<Score id: 1, query_id: 1, pull_id: 11, score: 3209891, created_at: "2010-11-18 04:19:43", updated_at: "2010-11-18 04:19:43">, #<Score id: 2, query_id: 2, pull_id: 11, score: 2890870, created_at: "2010-11-18 04:19:43", updated_at: "2010-11-18 04:19:43">, #<Score id: 3, query_id: 3, pull_id: 11, score: 1361203, created_at: "2010-11-18 04:19:43", updated_at: "2010-11-18 04:19:43">, #<Score id: 7, query_id: 4, pull_id: 13, score: 1382122, created_at: "2010-11-18 04:36:15", updated_at: "2010-11-18 04:36:15">]
>>

查看此错误报告:


非常感谢。但我在搜索中已经遇到了这个问题,它是特定于返回单个记录的命名范围的。