【发布时间】:2019-11-10 04:21:22
【问题描述】:
我有两个表:事务和属性。
我有一个条件要满足,即不需要加入表。
在我的交易查询中:
-
sales_date在某个月份的行 -
sold_or_leased被“租用”的行
我的下一个条件需要将属性加入交易,以便我可以:
-
transactions.sales_date在某个月份的行 -
transactions.sold_or_leased为空 AND 的行 -
properties.for_sale为假且properties.for_lease为真的行
基本上,在名为sold_or_leased 的事务中添加了一个新列,其中很多为空。我需要一个额外的查询来覆盖null 列。
#test variables for month
date = "2019-11-01"
month = Date.parse date
# below satisfies my first part
@testobj = Transaction.where(sold_or_leased: "leased")
.where("sales_date >= ? AND sales_date < ?", month.beginning_of_month, month.end_of_month).count
但现在我需要扩展此查询以包含属性并测试属性列
我不知道从这里去哪里:
@testobj = Transaction.joins(:property)
.where(sold_or_leased: "leased")
.where("sales_date >= ? AND sales_date < ?", month.beginning_of_month, month.end_of_month)
.or(
Transaction.where(sold_or_lease: nil)
).count
另外,当我添加一个连接然后添加一个 or 子句时,我收到一个错误 Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
我将分享相关型号信息:
交易模型:
class Transaction < ApplicationRecord
belongs_to :user
belongs_to :property
end
属性模型:
class Property < ApplicationRecord
has_one :property_transaction, class_name: 'Transaction', dependent: :destroy
end
在 Sebastian 的帮助下,我得到了以下信息(仍然会产生结构性错误消息):
Transaction.joins(:property)
.where(sales_date: month.all_month,
sold_or_leased: nil,
properties: { for_sale: false, for_lease: true })
.or(
Transaction.joins(:property)
.where(sold_or_leased: "leased")
.where("sales_date >= ? AND sales_date < ?", month.beginning_of_month, month.end_of_month)
)
【问题讨论】:
-
你的模型看起来怎么样?
-
我添加了一个模型外观示例。我剪掉了不相关的行
-
我测试了你的查询
Transaction.joins(:property).where(sales_date: month.all_month, sold_or_leased: nil, properties: { for_sale: false, for_lease: true }).or(Transaction.joins(:property).where(sold_or_leased: "leased").where("sales_date >= ? AND sales_date < ?", month.beginning_of_month, month.end_of_month))并且没有任何问题 1/2。 -
不错!慢慢来,检查一切是否按预期工作;)
-
@SebastianPalma 确认正在返回正确的数据:)。你是救生员!再次感谢!
标签: ruby-on-rails activerecord