Rails-连接多个表 [英] Rails - Joining multiple tables
本文介绍了Rails-连接多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下模型:
class Company < ActiveRecord::Base
has_many :price_movements
has_many :goods_movements
end
class PriceMovement < ActiveRecord::Base
belongs_to :company
end
class GoodsMovement < ActiveRecord::Base
belongs_to :company
end
我正在尝试加入一切都以activerecord的形式组合到一个sql中,但是我不确定该怎么做,因为我对ROR相对较新。
I am trying to join everything together into an sql in the form of activerecord, but I'm not sure how to go about doing it because I'm relatively new to ROR.
select * from companies c
inner join price_movements p
on c.id = p.company_id
inner join goods_movements g
on c.id = g.company_id
and g.date = p.date
我的主要问题是第二个链接,其中goods_movement date == price_movement日期。
有人可以建议是否有任何方法吗?
The key problem for me is the second link where goods_movement date == price_movement date. Can someone advice on whether there's any way to do it?
推荐答案
Company.joins(:price_movements,:goods_movements).where("goods_movement.date = price_movement.date")
通过此链接,其中详细说明了如何使用 ActiveRecord
Go through this link it has detailed explanation of how to use ActiveRecord
这篇关于Rails-连接多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文