简体   繁体   中英

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.

In the rails console I've tried various things but I keep getting this error

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "report"

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) }

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