I am experimenting with scope extensions and was wondering if I could do something like this.
class User
has_many :tasks
belongs_to :klass
scope :users_in_klass, lambda {|k| where(:klass_id => k)} do
def current_task_report
includes(:tasks)
users = []
each {|u|
user = {
:name => u.full_name,
:id => u.id,
:tasks => u.tasks
}
users << user
}
users
end
end
And call it like this
u = User.users_in_klass(6899)
u.current_task_report
The problem I’m having is that it’s ignoring the includes on the tasks for eager loading.
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE (`users`.`klass_id` = 6899)
Task Load (0.4ms) SELECT `tasks`.* FROM `tasks` WHERE (`tasks`.user_id = 46539)
Task Load (0.2ms) SELECT `tasks`.* FROM `tasks` WHERE (`tasks`.user_id = 46909)
Task Load (0.2ms) SELECT `tasks`.* FROM `tasks` WHERE (`tasks`.user_id = 46910)
Is what I’m am doing correct?
On a side note, Is there a better place to put the current_task_report method?
Cheers,
Tim
I think you need to move the includes statement into your lambda