开发者

Scoped query fails when same query from console works

开发者 https://www.devze.com 2023-01-25 22:56 出处:网络
Why would using a scoped query fail when querying directly works perfectly? My Model: class Score < ActiveRecord::Base

Why would using a scoped query fail when querying directly works perfectly?

My Model:

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

Firing up the console

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

but using that same query directly works without a hitch.

>> 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">, #&l开发者_运维百科t;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">]
>>


Check out this bug report:

https://rails.lighthouseapp.com/projects/8994/tickets/5960

0

精彩评论

暂无评论...
验证码 换一张
取 消