簡體   English   中英

Rails 4跨模型命名范圍

[英]rails 4 cross model named scopes

我要做的是創建適當的命名范圍,以獲取三天前創建的登錄用戶的所有報告。

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子句條目

帶有諸如

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

有任何想法嗎?

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