Ruby/ActiveRecord/Map to SQL
Материал из Wiki.crossplatform.ru
Содержание |
Aggregate function and group by
Account.find :all, :select => "COUNT(last_name) AS total, *", :group => "last_name" which would result in the following SQL: SELECT COUNT(last_name) AS total, * FROM accounts GROUP BY last_name
Group by clause
Account.find :all, :group => "last_name" will result in the following SQL: SELECT * FROM accounts GROUP BY last_name
Left join
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
Left join on a column
Account.find :all, :from => "visits LEFT JOIN accounts ON visits.account=accounts.id" And the following SQL query will result: SELECT * FROM visits LEFT JOIN accounts ON visits.account=accounts.id
Limit offset
Account.find :all, :limit => 10, :offset => 20 And the following SQL query will result: SELECT * FROM accounts LIMIT 10 OFFSET 20
The :order parameter defines the sorting order.
Account.find :all, :order => "created_on DESC, last_name, first_name" is SELECT * FROM accounts ORDER BY created_on DESC, last_name, first_name