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

时间:2011-02-05 10:04:18

标签: ruby-on-rails activerecord

Subscriber  
has_and_belongs_to_many :skills. 

Skill   
has_many :positions

在subscriber.rb中:

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

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

1 个答案:

答案 0 :(得分: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) }

并使用它:

@subscribers = notify_today(Time.now)