Ruby on rails rails 4跨模型命名范围

Ruby on rails rails 4跨模型命名范围,ruby-on-rails,named-scope,Ruby On Rails,Named Scope,我要做的是创建适当的命名作用域,以获取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

我要做的是创建适当的命名作用域,以获取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

有什么想法吗?

连接中,您必须像下面这样指定关联名称:

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

尝试
连接(:report).where(“reports.created_at>=?”,3.days.ago)
。数据库中的表名是什么?抱歉,我发了一个嘘声。我更新了它
评估报告
可能也需要在
中进行更改。取决于表名。
scope :submitted, -> { joins(:report).where("appraisal_reports.created_at >= ?", 3.days.ago) }