将SQL查询转换为ActiveRecord / Arel查询

时间:2015-11-05 17:36:34

标签: sql ruby-on-rails sql-server activerecord

我有一个SQL查询

SELECT 
  events.id
FROM
 events
   JOIN flows ON flows.event_id=events.id
   JOIN prices ON prices.priceable_id=flows.id AND prices.priceable_type='Flow'
GROUP BY
  events.id
HAVING SUM(prices.inclusive_tax)>200;

正在按预期工作。

我把它变成了一个查询:

Event.select('events.id').joins(:flows => :price).group('events.id').having('SUM(prices.inclusive_tax) > 200')

但我得到了ids。是否可以将其转换为ActiveRecord / Arel查询,以便我获得events条记录?

1 个答案:

答案 0 :(得分:0)

您可以尝试将查询修改为以下格式。这是我通常用于报告的内容

Event.find(:all, :conditions => condition_text, :from => from_text, :select => select_text, :joins => join_text)