Wow.. I used eager_loader and managed to do exactly what I wanted.
That's so awesome. There's no way I could have pulled that off with
ActiveRecord or DataMapper in such an elegant way. Here's what I did:
one_to_many :pstats, :class => :Ticket,
\
:eager_loader => (proc do |keys, records,
associations|
h =
{}
ids = records.collect { |r|
r.id }
# sum
hours
all =
Ticket.
group
(:project_id).
filter(:project_id =>
ids).
select(:project_id.as(:pid), :sum.sql_function(:hours).as
(:hours)).all
all.each do |
t|
h[t.pid] =
t.hours
end
records.each do |
r|
r.values[:hours] = h[r.id].try(:to_f) ||
0.0
end
end
Now I can just do Project.eager(:pstats) and then call p.hours (I use
missing_method to get to undefined attributes).
So nice :)
Thanks for the help!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---