Is there some sort of good pattern for dealing with computed values with Rails/ActiveRecord? A simple example: 3 tables, classes, students, and student_class_records (habtm style relation but contains a good deal of additional state so is modeled explicitly) Something like select the 10 classes with the most students. Easy enough in SQL. So I''m using a lot of find_by_sql and piggy-backing the computed values (number of students in this case). I am wrapping these in finder objects that at least encapsulate the raw SQL. But still it feels a little messy. Even worse is something like select the number of classes a teacher taught per semester. Again easy enough in SQL. But the returned rows don''t correspond to any model. I did use connection.select_all but render(:partial, :collection) will not accept a collection of hashes, so I''m stuffing it into a nearly unrelated ActiveRecord anyway to get the partial to render. I''m displaying the data in a data grid of sorts. The user can sort and filter by any visible field. Right now my solution is finder objects that mimic ActiveRecords'' find and count syntax as best they can. The finder objects shield the raw find_by_sql but it is still messy to set up. The finder is constructed with SQL fragments select, from, where, group by which it merges with the ActiveRecord style find( :conditions, :order ) to build the final SQL. It works but it''d be really nice if there were some Rails way to use associations to simplify this. Something like being able to sort or filter based on the number of a has_many association or sum or average some field of the has_many. So first, is there something I can do better now -- and two, can we get some more support built into ActiveRecord for this type stuff? Thanks. -- Jack Christensen jackc-/SOt/BrQZzOj3I+7jmQ39gC/G2K4zDHf@public.gmane.org