简体   繁体   中英

Ruby-on-Rails Where Clause with Parent Field

I have a basic data model with parent-child relationship between Import and ProductLicenseData defined as:

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

I want to write a method on ProductLicenseData that will return all "active" records, where "active" is defined as a record where "expirationDate" (a field in the database table the model represents) is greater than import's "extracted_at" value (a field on the database table that model represents). As such, I have something like

def self.active
  where("\"expirationDate\" > #{import.extracted_at}")

end

But this doesn't work. I have also tried:

def self.active
  ProductLicenseData.joins(:import).where("expirationDate > ?", import.extracted_at)
end

And just about every variation I can think of. Most end up with it telling me it doesn't know what "import" is, or "extracted_at". This seems like something that should be simple, but I am at a loss. How do I write this?

事实证明它是.joins(:import).where('"expirationDate" > imports.extracted_at') ,由于PostgreSQL中有虫子而产生了额外的引号...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM