【发布时间】:2018-01-08 20:11:17
【问题描述】:
我正在努力在我的 Rails 应用程序中返回两个范围的联合或将它们合并为一个。我正在寻找的结果是检索满足以下任一范围的所有订单:Order.with_buyer_like 或 Order.with_customer_like。那就是我正在寻找集合的并集。重要的是,我需要将这些作为 ActiveRecord::Relation 而不是数组返回。否则,我就这么做了
results = Order.with_buyer_like + Order.with_customer_like
我也尝试使用“或”运算符,但出现错误:
Order.with_seller_like('pete').or(Order.with_customer_like('pete'))
ArgumentError: Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
我也尝试过将这些作用域合二为一,但我不能完全让它发挥作用。
这是我的设置:
class Company
has_many :stores
end
class Store
belongs_to :company
end
class Order
belongs_to :buying_store, class_name: 'Store', foreign_key: 'buying_store_id', required: true
belongs_to :selling_store, class_name: 'Store', foreign_key: 'selling_store_id', required: true
scope :with_buyer_like, ->(search_term) { joins(buying_store: [:company], :customer).where(['stores.name LIKE ? OR companies.name LIKE ?', "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%"]) }
scope :with_customer_like, ->(search_term) { joins(:customer).where(['first_name LIKE ? OR last_name LIKE ? OR mobile_number LIKE ? OR email LIKE ?', "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%"] ) }
end
【问题讨论】:
-
错误表示 or 中的连接不兼容(
joins在您的范围内创建) -
你试过
Order.with_buyer_like('pete').with_customer_like('pete')吗? -
m.simon,问题在于它给了我集合的交集,而不是联合。
-
Yoshiji 先生 - 我只是不知道该怎么办。每个范围都可以完美地工作。
标签: ruby-on-rails postgresql activerecord arel