1

我似乎找不到以下查询的“Rails-Way”:

这是我的模型定义:

class Backend::Order < ActiveRecord::Base
  has_one :backend_order_filter, :class_name => 'Backend::OrderFilter', :dependent => :destroy

class Backend::OrderFilter < ActiveRecord::Base
  belongs_to :backend_order, :class_name => 'Backend::Order', :foreign_key => 'order_id'

这是我的原始工作 sql 查询:

Backend::Order.find_by_sql(
    "SELECT `backend_orders`.*,`backend_order_filters`.*,`backend_customers`.*
      FROM `backend_orders`
      LEFT JOIN `backend_order_filters` ON (`backend_orders`.`id` = `backend_order_filters`.`order_id`)
      WHERE
          (`backend_orders`.`order_status` IN ('OK', 'AO') OR `backend_orders`.`order_status` IS NULL) AND
          (`backend_order_filters`.`financing` = 1 OR (`backend_orders`.`price` - `backend_orders`.`balance`) <= 0)AND
          `backend_orders`.`delivery_number` IN ('21','22') AND (`backend_order_filters`.`places_of_delivery` = 1)"
)

由于“Backend::Order”和“Backend::OrderFilter”之间的 OR 运算符,我尝试使用 Arel-tables 执行此操作,但我找不到解决方案。我还在 Stack Overflow 上搜索过类似的问题,但不幸的是我运气不好。所有关于 Arel 表的答案都只考虑一个没有关联的主要模型。感谢您的任何帮助。

4

0 回答 0