Prev | Current Page 100 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

In this case, it is the GROUP BY clause
of the SQL statement. The Active Record find call
Account.find :all, :group => "last_name"
will result in the following SQL:
SELECT * FROM accounts GROUP BY last_name
It will most often be the case that you will use the :group option in concert with the
:select option to define some additional grouped parameters. For instance, if you wanted to
find the number of people who have the same last name, you could do the following
Account.find :all, :select => "COUNT(last_name) AS total, *", :group => "last_name"
CHAPTER 2 ?–  ACTIVE RECORD AND SQL 31
which would result in the following SQL:
SELECT COUNT(last_name) AS total, * FROM accounts GROUP BY last_name
:joins
While the join parameter is similar in function to the :include option, it works on a lower level
in the resulting SQL statement. The value given to the :joins option is a string that will get
added to the FROM clause of the SQL statement. You can use this to perform a join on tables to
which you don??™t have a defined Active Record relationship.


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