简体   繁体   English

Rails 4跨模型命名范围

[英]rails 4 cross model named scopes

What I want to do is create appropriate named scopes to get all reports of a logged in user that were created 3 days ago 我要做的是创建适当的命名范围,以获取三天前创建的登录用户的所有报告。

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

I've tried different rails console statements but perhaps I'm going about it the wrong way. 我尝试了不同的Rails控制台语句,但也许我正在以错误的方式进行操作。

In the rails console I've tried various things but I keep getting this error 在Rails控制台中,我尝试了各种方法,但一直出现此错误

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "report" PG :: UndefinedTable:错误:缺少表“报告”的FROM子句条目

with statements such as 带有诸如

user = User.find(2)
user.appraisals.submitted

Any ideas? 有任何想法吗?

joins您必须指定关联名称,如下所示:

scope :submitted, -> { joins(:report).where("appraisal_reports.created_at >= ?", 3.days.ago) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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