将多态作用域与或组合

wtlkbnrh  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(234)

当两个作用域都是多态关联时,如何在一个查询中组合两个作用域以获得它们的两个结果?我有这样的模型:

class Vendor < ApplicationRecord
  has_many :shipping_method_registers, as: :shipper
  has_many :shipping_methods, through: :shipping_method_registers
end

class City < ApplicationRecord
  has_many :shipping_method_registers, as: :shipper
  has_many :shipping_methods, through: :shipping_method_registers
end

class ShippingMethodRegister < ApplicationRecord
  belongs_to :shipping_method
  belongs_to :shipper, polymorphic: true
end

class ShippingMethod < ApplicationRecord
  has_many :shipping_method_registers, dependent: :destroy, autosave: true
  has_many :city_zones, through: :shipping_method_registers, source: :shipper,
                        source_type: "City"
  has_many :vendors, through: :shipping_method_registers, source: :shipper,
                     source_type: "Vendor"
end

我想得到一个供应商和一个城市的运输方式相结合,但只有不同的结果。我试过了

> vendor.shipping_methods.or(city.shipping_methods)
ArgumentError: Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
from /usr/local/bundle/gems/activerecord-5.2.0/lib/active_record/relation/query_methods.rb:634:in `or!'

还有

> vendor.shipping_methods.merge(city.shipping_methods)

但这只返回城市的运输方式。我可以在ruby中合并它们,但我想在一个查询中完成。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题