0
我有一個運行,並從MySQL返回所需的結果下面的SQL語句:Rails 3的範圍與SQL返回空結果
select event_id, count(*) as num_bookmarks
from event_connections join events on events.id=event_connections.event_id
where event_connections.connectiontype = 7
我試圖創建範圍與此,所以我做了以下。 ..
scope :by_bookmark_count, select("event_id, count(*) as num_bookmarks").
from("event_connections").
joins("join events on events.id=event_connections.event_id where event_connections.connectiontype = 3").
group("event_id")
當我從控制檯運行我得到這個Event.by_bookmark_count
...
2.1.5 :001 > Event.by_bookmark_count
Event Load (12.4ms) SELECT event_id, count(*) as num_bookmarks FROM event_connections join events on events.id=event_connections.event_id where event_connections.connectiontype = 7 GROUP BY event_id
=> [#<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >, #<Event >...
請注意SQL是正確的,但沒有結果被返回。我錯過了什麼?