我有一个基本数据模型,其中Import和ProductLicenseData之间的父子关系定义为:
class Import < ActiveRecord::Base
has_many :product_license_data, class: ProductLicenseData, dependent: :destroy
att_reader :extract_at
end
class ProductLicenseData < ActiveRecord::Base
belongs_to :import
end
我想在ProductLicenseData上编写一个方法,它将返回所有&#34; active&#34;记录,其中&#34;活跃&#34;被定义为记录,其中&#34; expirationDate&#34; (模型表示的数据库表中的字段)大于import&#34; extracted_at&#34; value(模型表示的数据库表上的字段)。因此,我有类似
的东西def self.active
where("\"expirationDate\" > #{import.extracted_at}")
end
但这不起作用。我也尝试过:
def self.active
ProductLicenseData.joins(:import).where("expirationDate > ?", import.extracted_at)
end
几乎我能想到的每一个变化。大多数人最终会告诉我它并不知道&#34; import&#34;是,或&#34; extract_at&#34;。这似乎应该很简单,但我不知所措。我怎么写这个?
答案 0 :(得分:1)
事实证明它是.joins(:import).where('"expirationDate" > imports.extracted_at')
,由于PostgreSQL中的昆虫引起了额外的引用......