I’m writing a rails finder query that pulls out records for articles sorted by the highest numbers of approved and public comments per article.
Which is the best way to run such a query in Ruby on Rails?
As a general rule, are developers in favour of in-memory filtering or running complex SQL queries?
Letting the database do its job is always best. Just write the query with a where clause/join/etc. and use a sort by to return only the records you need. By returning more records and then filtering the data in rails wastes the amount of the data across the “wire” (between DB and app server) and also memory. Databases are optimized for these sorts of things. Use an index on the table as necessary.
Another similar post: http://www.mail-archive.com/rubyonrails-talk@googlegroups.com/msg13484.html