In SQL, this would happen with the SELECT statement, and with Active Record, you use the
find method. The definition of the find method is rather ambiguous, so looking at the names
of the arguments isn??™t going to help. As it turns out, find has a few different ways of getting
your data back to you.
It??™s probably best if we deal with find as acting like two different methods: one that will
retrieve objects with the specified IDs (which is simple) and one that will find objects with
a more complicated set of criteria. The first form of the find method takes a list of one or more
integers:
book = Book.find 5
This invocation of find will query the database for the row that has an ID column equal to
5. Specifically, it is equivalent to
SELECT* FROM books WHERE (id = 5)
Similarly, you can give an array of integers to this form of find, and it will return all of the
corresponding rows. For example, the following invocation of find
books = Book.find 1, 3, 5, 7, 9
will generate the equivalent of the following SQL statement:
SELECT * FROM books WHERE (id IN ( 1, 3, 5, 7, 9 ))
The find method possesses quite a lot more power than simply fetching a list of IDs.
Pages:
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104