我有一个模型事件,它取决于执行,这取决于具有ProductTanslation的产品。 同样的事件模型有很多订单。 我想在表格中显示事件,并能够按计数(订单)对它们进行排序,按名称搜索... 我发了sql请求
select e.id, pt.name, nbc.nb
from `events` as e
inner join `executions` as ex on e.`execution_id` = ex.`id`
inner join `products` as p on ex.`product_id` = p.`id`
left outer join (select product_id, `name` from `product_translations` where `locale` ='en' group by `product_id` ) as pt on pt.`product_id` = p.id
left outer join (select event_id, COUNT(*) as nb from orders group by event_id) as nbc on nbc.event_id = e.id
where pt.name like '%plane%'
order by 3 desc
但我无法使用ActiveReccord特别按顺序(订单)
转录它顺便说一句,我使用gem globalize进行翻译。
答案 0 :(得分:3)
将SQL查询转换为Rails的方法:
初始请求
select e.id, pt.name, nbc.nb
from `events` as e
inner join `executions` as ex on e.`execution_id` = ex.`id`
inner join `products` as p on ex.`product_id` = p.`id`
left outer join (select product_id, `name` from `product_translations` where `locale` ='en' group by `product_id` ) as pt on pt.`product_id` = p.id
left outer join (select event_id, COUNT(*) as nb from orders group by event_id) as nbc on nbc.event_id = e.id
where pt.name like '%plane%'
order by 3 desc
第一步
确定主表;在这里,它是Event
。所以这是来自Event
模型的电话。
Event
.select("events.id, pt.name, nbc.nb")
.joins("inner join `executions` as ex on events.`execution_id` = ex.`id`
inner join `products` as p on ex.`product_id` = p.`id`
left outer join (select product_id, `name` from `product_translations` where `locale` ='en' group by `product_id` ) as pt on pt.`product_id` = p.id
left outer join (select event_id, COUNT(*) as nb from orders group by event_id) as nbc on nbc.event_id = events.id")
.where("pt.name like '%plane%'")
.order("3 desc")
第二步将内部联接转换为关系。您必须向has_many
/ belongs_to
申报Execution
和Product
。
Model Event < AR::Base
has_many :executions
has_many :products, :through => :executions
Event
.select("events.id, pt.name, nbc.nb")
.joins(:products)
.joins("left outer join (select product_id, `name` from `product_translations` where `locale` ='en' group by `product_id`) as pt on pt.`product_id` = products.id
left outer join (select event_id, COUNT(*) as nb from orders group by event_id) as nbc on nbc.event_id = events.id")
.where("pt.name like '%plane%'")
.order("3 desc")
子查询目前无法使用Rails 4完成左外连接,但您可以提取子查询。
sub_query1 = ProductTranslation
.select("product_id, `name`")
.where("`locale` ='en'")
.group("`product_id`")
sub_query2 = Order
.select("event_id, COUNT(*) as nb")
.group("event_id")
Event
.select("events.id, pt.name, nbc.nb")
.joins(:products)
.joins("left outer join (#{sub_query1.to_sql}) as pt on pt.`product_id` = products.id
left outer join (#{sub_query2.to_sql}) as nbc on nbc.event_id = events.id")
.where("pt.name like '%plane%'")
.order("3 desc")
其他一些更正 SQL命令是大写的,条件是散列,更喜欢将table_name用于表模型。
sub_query1 = ProductTranslation
.select("`product_id`, `name`")
.where({ :locale => 'en' })
.group(:product_id)
sub_query2 = Order
.select("event_id, COUNT(*) as nb")
.group("event_id")
Event
.select("#{Event.table_name}.id, pt.name, nbc.nb")
.joins(:products)
.joins("LEFT OUTER JOIN (#{sub_query1.to_sql}) AS pt
ON pt.`product_id` = #{Product.table_name}.id
LEFT OUTER JOIN (#{sub_query2.to_sql}) AS nbc
ON nbc.event_id = #{Event.table_name}.id")
.where("pt.name LIKE ?", '%plane%')
.order("3 DESC")
Voilà!