Wednesday, February 7, 2007

Direct SQL Access from within Rails

Rails is awesome, but as much as it hides the database, there are applications and situations you run into every so often where you really do need direct access to SQL. What do you do?

It turns out the answer is simple. First, any ActiveRecord object will have a connection you can access, and every connection will have an execute method you can use to execute any arbitrary SQL.

It's as easy as ArbitraryModel.connection.execute("some sql where * etc;").

Found this out from (of course) Mr. Jamis Buck, and verified it with a peek at the API docs.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.