Michael Schuerig
2005-Sep-26 23:19 UTC
[Rails-core] Redefining AR::Base#find_with_associations?
I''m trying to redefine the #find_with_associations method mixed into ActiveRecord::Base from ActiveRecord::Associations. The rationale is that the original method does not support offset/limit when to-many associations are included. That''s decidedly not POLS-compliant. As I need pagination for that kind of queries, too, my first approach was to handle the case externally, by finding all objects and keeping only the desired ones. What can I say? It works, but is aesthetically displeasing. As a remedy, I have implemented limit/offset using a sub-select on top of the [*] monster patch. I''m not yet convinced that it works and what to test for, but the results look right. For now, I have changed find_with_association another time, but in contrast to earlier changes this one relies on a feature not available in all DBMS supported by Rails. Thus, given the desirability of offset/limit for all kinds of queries, there need to be two implementations. The sub-select variant for the more capable DBMSs, another one that picks the desired objects from the entire result set, for their poorer cousins. Using [*], the latter approach isn''t too bad, as it is not necessary to instantiate unneeded objects. Finally, to the point of the subject. I''ve tried to sneak in the sub-select implementation by redefining find_with_associations, but the thing eludes me. Michael [*] http://dev.rubyonrails.org/ticket/2172 -- Michael Schuerig There is no matrix, mailto:michael@schuerig.de only reality. http://www.schuerig.de/michael/ --Lawrence Fishburn