find to create a join:
logins_from_illinois = User.find(:all, :include => :person,
:conditions => "people.state = 'IL'").map(&:login)
Note the pluralization: in the :include parameter, we use the singular, as it is the
name of the association (a User has-one Person). But since the :conditions are
directly injected into the SQL statement, the table name is used (... WHERE people.
state = 'IL').
Indexing
Another area in which you have to be careful of your database is indexing. Improper
indexing is a common problem; indexing is easily overlooked when creating a database,
and you often do not see the results until there is a major performance impact
on the application.
Unfortunately, this is an area where Rails does not (and cannot) help you. Indexing is
quite application-specific. Other than primary keys (which are usually automatically
indexed by the database), you must remember to create the necessary indexes yourself.
* The actual queries have been simplified for clarity. Most of the Rails database adapters reflect on the database??™s
metadata for information about available tables, columns, and data types.
168 | Chapter 6: Performance
There is really no substitute for understanding what queries are being sent to the
database and how they are being satisfied.
Be sure to think critically about where you need indexes, as well as where you don??™t
need them. In particular, you should omit indexes on tables that are very infrequently
read from (such as audit logs).
Pages:
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260