Or of course for the sake on simplicity in this case you could just
write find_by_sql instead of find, i.e.
If my model was User and therefore my table was users.
User.find(:all) # => SELECT * FROM users;
is the same as writing
User.find_by_sql("SELECT * FROM users");
Of course this is stupid and you wouldn't bother for such a simple
statement, but sometimes Active Record( that's the Rails gem that
writes all this sql etc for you ) does do stupid and very ineffecient
Here is something from my code base used to make a google sitemap
# AR was doing a ton of SQL to get these records!
# With AR => DB: 3.31273 (142%)
# With find_by_sql => DB: 0.47021 (12%)
@products = Product.find_by_sql('SELECT p.permalink, p.updated_at,
c.permalink AS cat_link FROM products AS p INNER JOIN categories AS c
ON c.id = p.category_id')
Note the massive difference in db times, from Active Record as opposed
to rolling your own. Of course these are generally corner cases, and
it's just worth watching your logs when you've got complex
relationships going on.
I'm not bagging AR, it rulZ mate, but sometimes it's not as clever as
we people are....