【发布时间】:2020-03-11 01:17:28
【问题描述】:
我有一个链式范围查询,它检查标记项目的标签并将它们从“可用”列表中删除。但是,此范围被固定到“可见”范围:
scope :visible, -> { where("rfid_tags.location_id NOT IN (SELECT warehouses.id FROM warehouses WHERE warehouses.deleted_at IS NULL AND warehouses.hidden = 't') OR rfid_tags.location_type = 'Listing'") }
scope :available, -> { not_selected.visible.without_flags }
scope :without_flags, -> { joins("LEFT OUTER JOIN flags ON flags.rfid_tag_id = rfid_tags.id").where(flags: { id: nil })}
我正在尝试对可见标志进行排序,以便标记的项目位于列表的末尾;一位同事建议我使用类似的东西:
scope :available, -> { not_selected.visible.without_flags.ordered }
scope :ordered, -> (recs) { recs.order('flags.rfid_tag_id NULLS FIRST') }
并将其附加到 :visible,但我认为我不太了解如何使用 recs 概念(研究如何传入参数)?尝试在 :ordered 范围上加入标志会因为多次调用标志表而出错。我正在使用 Rails 4/postgresql。如何使用连接表数据正确结算排序?
【问题讨论】:
标签: postgresql ruby-on-rails-4 activerecord scope