【发布时间】:2019-07-28 03:38:24
【问题描述】:
我有一个Review 模型和一个CheckIn 模型:
class Review < ApplicationRecord
belongs_to :reviewable, polymorphic: true
belongs_to :check_in, -> { where( reviews: { reviewable_type: "CheckIn"} ).includes(:review) }, foreign_key: 'reviewable_id', optional: true
end
class CheckIn < ApplicationRecord
has_one :review, as: :reviewable
end
并且希望能够在一个日期范围内为:check_ins 提取所有:reviews。例如:
Review.includes(:check_in).where("check_ins.created_at >= ?", Date.today)
由于以下原因导致失败:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "check_ins"
有没有办法做到这一点?理想情况下,我想要一个 ActiveRecord::Relation 而不是数组。
【问题讨论】:
标签: ruby-on-rails polymorphism associations