Low-Level ActiveRecord
With many Rails projects, there is occasionally a need to bypass the normal ActiveRecord API, and get closer to the low-level SQL API. Here are a few useful methods that bypass the confines of everyday ActiveRecord.
#find_by_sql users = User.find_by_sql("SELECT * FROM users WHERE id = 1") This returns model instances for the given SQL string.
#find_by_sql also accepts parametrized values, like this:
users = User.find_by_sql(["SELECT * FROM users WHERE id = ?