简体   繁体   中英

Rails - Joining multiple tables

I have the following models:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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