SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 101 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

While such a join may not be something
that you will use on a normal basis, it can be very powerful in the situations where you
do need it.
For example, if you have a legacy table named visits that does not have a defined relationship
to your Active Record model??™s table and you need to join against and select out of
visits, your code might look something like the following:
Account.find :all,
:joins => "LEFT JOIN visits ON accounts.id=visits.account"
The result will be the following SQL query:
SELECT * FROM accounts LEFT JOIN visits ON accounts.id=visits.account_id
As you can see, the value given to the :joins option is appended to the FROM portion of the
SQL; it does not replace it. Therefore, your model??™s table, in this case accounts, will always be
included in the FROM clause. If you don??™t want your model??™s table to be included, you can use
the :from option, described next??”but, in that case, you also should probably consider just
querying the database directly, using either the find_by_sql or execute methods, described
later in this chapter.


Pages:
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113