diff options
author | Akira Matsuda <ronnie@dio.jp> | 2011-06-29 08:29:34 +0900 |
---|---|---|
committer | Akira Matsuda <ronnie@dio.jp> | 2011-06-29 08:36:04 +0900 |
commit | 7d3a61a0e2bbcd6cf4fbeaa6aec5fc1d52a389d4 (patch) | |
tree | cd9f5837213afa799ab50e32fc87b26f331dd8e8 | |
parent | cfab51c819111d1dff3e6094e49ed33bc9262305 (diff) | |
download | rails-7d3a61a0e2bbcd6cf4fbeaa6aec5fc1d52a389d4.tar.gz rails-7d3a61a0e2bbcd6cf4fbeaa6aec5fc1d52a389d4.tar.bz2 rails-7d3a61a0e2bbcd6cf4fbeaa6aec5fc1d52a389d4.zip |
correct invalid HAVING query
* GROUP BY value must appear in SELECT clause
* ("created_at < ?", 1.month.ago) wasn't a very good example for HAVING. you'd better use WHERE for such a simple condition
-rw-r--r-- | railties/guides/source/active_record_querying.textile | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 93a98755a2..8937a0c172 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -484,16 +484,16 @@ SQL uses the +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You For example: <ruby> -Order.group("date(created_at)").having("created_at < ?", 1.month.ago) +Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)").having("sum(price) > ?", 100) </ruby> The SQL that would be executed would be something like this: <sql> -SELECT * FROM orders GROUP BY date(created_at) HAVING created_at < '2011-04-27' +SELECT date(created_at) as ordered_date, sum(price) as total_price FROM orders GROUP BY date(created_at) HAVING sum(price) > 100 </sql> -This will return single order objects for each day, but only those that are at least one month old. +This will return single order objects for each day, but only those that are ordered more than $100 in a day. h3. Overriding Conditions |