Activerecord条件和命名范围 - 未定义方法`created_at'

Mar*_*rov 1 activerecord ruby-on-rails

Subscriber  
has_and_belongs_to_many :skills. 

Skill   
has_many :positions
Run Code Online (Sandbox Code Playgroud)

在subscriber.rb中:

scope :notify_today, 
      includes(:skills => :positions).
      where("positions.created_at > ? AND positions.created_at > ?", 
             1.day.ago, self.created_at)
Run Code Online (Sandbox Code Playgroud)

基本上我想找到所有拥有位置的订户1)创建1.day.ago AND 2)在订阅者之后创建

Zab*_*bba 5

发生错误是因为self这里的类Class不是Subscriber所希望的.

作为一个解决方案,你可以像这样制作一个lamda并传入参数created_at:(我认为你的范围是有效的,因为我没有专门用你的范围代码测试它)

scope :notify_today, 
      lambda { |created_at| includes(:skills => :positions).
      where("positions.created_at > ? AND positions.created_at > ?", 
             1.day.ago, created_at) }
Run Code Online (Sandbox Code Playgroud)

并使用它:

@subscribers = notify_today(Time.now)
Run Code Online (Sandbox Code Playgroud)