Prev | Current Page 113 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

So be careful about what data
you??™re expecting to return.
Because the find_by_sql method will create objects from the return value of the SQL, you
cannot use it to execute any bit of arbitrary SQL. Therefore, if you are seeking to execute truly
custom SQL statements, including INSERT and UPDATE statements, the execute method is what
you??™d want to use. You can do anything with the database that you??™d need to (except, of course,
return fields) through this function.
Transactions
Many databases support the idea of transactions, that is, if there is an error performing
a statement, which occurs within a specified block, the database will be rolled back to the
state it was in before the block of statements.
A simple example for transactions is when you need to perform two actions, the second of
which should not occur if there is a problem performing the first. The classic example is a bank
account transfer. In SQL, an account transfer would be performed in the following manner:
UPDATE accounts SET balance=balance-300.


Pages:
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125