【发布时间】:2026-02-04 03:30:02
【问题描述】:
我想要做的是创建适当的命名范围来获取 3 天前创建的登录用户的所有报告
class User < ActiveRecord::Base
has_many :appraisals
end
class Appraisal < ActiveRecord::Base
belongs_to :user
has_one :report
scope :submitted, -> { joins(:report).where("reports.created_at >= ?", 3.days.ago) }
end
class Report < ActiveRecord::Base
belongs_to :appraisal
end
我尝试了不同的 Rails 控制台语句,但也许我用错了方法。
在 Rails 控制台中,我尝试了各种方法,但一直收到此错误
PG::UndefinedTable: 错误:表“报告”缺少 FROM 子句条目
with 语句如
user = User.find(2)
user.appraisals.submitted
有什么想法吗?
【问题讨论】:
-
试试
joins(:report).where("reports.created_at >= ?", 3.days.ago)。你在数据库中的表名是什么? -
抱歉,我发出了嘘声。我更新了它