ActiveRecord的条件和命名范围 - 未定义的方法'created_at'属性 [英] Activerecord conditions and named scope - undefined method `created_at'

查看:91
本文介绍了ActiveRecord的条件和命名范围 - 未定义的方法'created_at'属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Subscriber  
has_and_belongs_to_many :skills. 

Skill   
has_many :positions

在subscriber.rb:

in 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; 2)用户后创建

Basically I want to find all subscribers that have positions 1) created 1.day.ago AND 2) created after the subscriber

推荐答案

出现的错误,因为该类这里是,不是用户,如想。

The error occurs because the class of self here is Class, not Subscriber, as is wanted.

作为一个解决方案,可以使LAMDA这样并传入参数 created_at : (我presume的范围进行工作的,因为我没有测试用的的范围code专)

As a solution, you can make a lamda like this and pass in the parameter for created_at: (I presume your scope is working otherwise, because I have not tested this with your scope code specifically)

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)

这篇关于ActiveRecord的条件和命名范围 - 未定义的方法'created_at'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆