当来自控制台的相同查询工作时,范围查询失败

时间:2010-11-18 13:22:10

标签: 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

启动控制台

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">]
>>

1 个答案:

答案 0 :(得分:0)