在我的用户模型类中,我定义了 2 个范围,如下所示。
scope :condition, lambda { where('updated_at >= ?', 3.day.ago) }
scope :tardy, lambda {
joins(:timesheets).group("users.id") & Timesheet.condition
}
然后我尝试运行重新加载!,然后u =User.find 14我得到下面的结果。
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 14 LIMIT 1
=> #<User id: 14, login: "janu", password: "janu", password_confirmation: nil, email: "janu@gmail.com", created_at: "2013-01-03 09:47:36", updated_at: "2013-01-03 09:47:36">
然后我运行u.tardy.to_sql,但是它返回以下错误。
NoMethodError: undefined method `tardy' for #<User:0x00000003cb5ea0>
from /home/local/rajesh.co/.rvm/gems/ruby-1.9.3-p327/gems/activemodel-3.2.9/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /home/local/rajesh.co/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_methods.rb:149:in `method_missing'
from (irb):64
from /home/local/rajesh.co/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/local/rajesh.co/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/local/rajesh.co/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
看,当我如下所示将两个范围连接在一起时,我也会遇到同样的错误。
scope :tardy, lambda {
joins(:timesheets).
where("timesheets.updated_at >= ?", 3.days.ago).
group("users.id")
}
你能帮我解决同样的问题吗?谢谢你。