This SQL is grouped inside parentheses in the final query, so you don??™t have to worry
about a stray OR statement ruining your results. In the following example, the Array form of the
conditions parameter is used in a query to find the records that have been recently updated,
or those in the included set of IDs:
Account.find :all, :conditions => [ "updated_on > ? OR id IN (?)",
@last_update,
[ 2, 3, 5, 7, 11 ] ]
It will result in the following SQL:
SELECT *
FROM accounts
WHERE
updated_on > '20061214 15:29:12'
OR id IN (2, 3, 5, 7, 11)
As mentioned earlier, Active Record will interpolate the values supplied as conditions for
you, making sure that they are quoted properly to avoid any potential issues, both accidental
CHAPTER 2 ?– ACTIVE RECORD AND SQL 28
and intentional. It will also make sure to properly convert data types, like the date used in the
previous example, into whatever format your database expects.
:include
Here, we have the FROM clause in your SQL statement. The :include parameter will take a (potentially
very) nested hash of symbols that corresponds to the names of relationships you??™ve defined
for your model, and add them as joining conditions.
Pages:
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107