ruby on rails 3 scope extensions and includes -
i experimenting scope extensions , wondering if this.
class user has_many :tasks belongs_to :klass scope :users_in_klass, lambda {|k| where(:klass_id => k)} 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 this
u = user.users_in_klass(6899) u.current_task_report
the problem i'm having it's ignoring includes on tasks eager loading.
user load (0.5ms) select `users`.* `users` (`users`.`klass_id` = 6899) task load (0.4ms) select `tasks`.* `tasks` (`tasks`.user_id = 46539) task load (0.2ms) select `tasks`.* `tasks` (`tasks`.user_id = 46909) task load (0.2ms) select `tasks`.* `tasks` (`tasks`.user_id = 46910)
is i'm doing correct?
on side note, there better place put current_task_report method?
cheers,
tim
i think need move includes statement lambda
class user has_many :tasks belongs_to :klass scope :users_in_klass, lambda {|k| includes(:tasks).where(:klass_id => k)} def current_task_report users = [] each {|u| user = { :name => u.full_name, :id => u.id, :tasks => u.tasks } users << user } users end end
Comments
Post a Comment