开发者

Writing an ActiveRecord scope with a join and "OR" condition

I have two tables: shipments and manifests.

  • shipments: id, name
  • manifests: id, shipment_id, start_time, end_time

A Shipment has_many Manifests, and a Manifest belongs_to one Shipment.

How do I write an ActiveRecord scope that returns me all the Shipments where:

  1. if it has at least 开发者_开发技巧one manifest, the current time is between the start and end times, OR
  2. it has no manifests


Hi I'm not sure if it can be achieved through scopes in ruby it may be sth like

   shipments = Shipments.all( :include => :manifests )
   empty_shipments = shipments.select { |item| item.manifests.blank? }
   non_empty_shipments = shipments - empty_shipments
   non_empty_shipments.delete_if {|item| (item.t_start..item.t_end).cover? Time.now}
   empty_shipments & non_empty_shipments
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜