In my User model class, I have defined 2 scopes as given below.
scope :condition, lambda { where('updated_at >= ?', 3.day.ago) }
scope :tardy, lambda {
joins(:timesheets).group("users.id") & Timesheet.condition
}
Then I tried running reload!, and thereafter u =User.find 14 and I got result below.
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: "[email protected]", created_at: "2013-01-03 09:47:36", updated_at: "2013-01-03 09:47:36">
Then I run, u.tardy.to_sql,but, it returns the following error.
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>'
See, When I join both the scopes together as given below, then also I am getting the same error.
scope :tardy, lambda {
joins(:timesheets).
where("timesheets.updated_at >= ?", 3.days.ago).
group("users.id")
}
Can you please help me to resolve the same. Thank you.