繁体   English   中英

Rails - 连接多个表

[英]Rails - Joining multiple tables

我有以下型号:

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相对较新。

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

对我来说,关键问题是货物运动日期== price_movement日期的第二个环节。 有人可以建议是否有办法吗?

Company.joins(:price_movements,:goods_movements).where("goods_movement.date = price_movement.date")

通过此链接,它详细解释了如何使用ActiveRecord

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM