将mysql查询转换为rails范围

时间:2014-03-18 14:59:02

标签: mysql ruby-on-rails-4

我希望将这个mysql查询转换为名为stats的模型中的rails范围。以下是预期结果的示例。任何帮助将不胜感激。

  scope :by_content, -> {
    select("date, title").
    group(:title).
    order("title")
  }


SELECT s.date as date, s.title as title,
  count(F.id) as impressions,
  count(D.event) as completed
FROM stats s
  Left Join stats as F on s.id = F.id and F.event ='play' and F.videopos = '0'
  Left Join stats as D on s.id = D.id and D.event = 'completed'
WHERE s.user_id = '2'
AND (s.date_time between '2014-03-08 00:00:00' and '2014-03-18 23:59:59')
AND s.title is not null
GROUP BY s.title

0 个答案:

没有答案