Skip to content
This repository
Browse code

Add :having option to find, to use in combination with grouped finds.…

… Also added to has_many and has_and_belongs_to_many associations.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1028 state:committed]
  • Loading branch information...
commit 97403ad5fdfcdfb2110c6f8fd0ebf43b7afc4859 1 parent 0c4ba90
Emilio Tagua authored November 21, 2008 NZKoz committed December 01, 2008
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *2.3.0/3.0*
2 2
 
  3
+* Add :having as a key to find and the relevant associations.  [miloops]
  4
+
3 5
 * Added default_scope to Base #1381 [Paweł Kondzior]. Example:
4 6
 
5 7
     class Person < ActiveRecord::Base
12  activerecord/lib/active_record/associations.rb
@@ -724,6 +724,8 @@ module ClassMethods
724 724
       #   Specify second-order associations that should be eager loaded when the collection is loaded.
725 725
       # [:group]
726 726
       #   An attribute name by which the result should be grouped. Uses the <tt>GROUP BY</tt> SQL-clause.
  727
+      # [:having]
  728
+      #   Combined with +:group+ this can be used to filter the records that a <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause.
727 729
       # [:limit]
728 730
       #   An integer determining the limit on the number of rows that should be returned.
729 731
       # [:offset]
@@ -1181,6 +1183,8 @@ def belongs_to(association_id, options = {})
1181 1183
       #   Specify second-order associations that should be eager loaded when the collection is loaded.
1182 1184
       # [:group]
1183 1185
       #   An attribute name by which the result should be grouped. Uses the <tt>GROUP BY</tt> SQL-clause.
  1186
+      # [:having]
  1187
+      #   Combined with +:group+ this can be used to filter the records that a <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause.
1184 1188
       # [:limit]
1185 1189
       #   An integer determining the limit on the number of rows that should be returned.
1186 1190
       # [:offset]
@@ -1553,7 +1557,7 @@ def nullify_has_many_dependencies(record, reflection_name, association_class, pr
1553 1557
         @@valid_keys_for_has_many_association = [
1554 1558
           :class_name, :table_name, :foreign_key, :primary_key,
1555 1559
           :dependent,
1556  
-          :select, :conditions, :include, :order, :group, :limit, :offset,
  1560
+          :select, :conditions, :include, :order, :group, :having, :limit, :offset,
1557 1561
           :as, :through, :source, :source_type,
1558 1562
           :uniq,
1559 1563
           :finder_sql, :counter_sql,
@@ -1609,7 +1613,7 @@ def create_belongs_to_reflection(association_id, options)
1609 1613
         mattr_accessor :valid_keys_for_has_and_belongs_to_many_association
1610 1614
         @@valid_keys_for_has_and_belongs_to_many_association = [
1611 1615
           :class_name, :table_name, :join_table, :foreign_key, :association_foreign_key,
1612  
-          :select, :conditions, :include, :order, :group, :limit, :offset,
  1616
+          :select, :conditions, :include, :order, :group, :having, :limit, :offset,
1613 1617
           :uniq,
1614 1618
           :finder_sql, :counter_sql, :delete_sql, :insert_sql,
1615 1619
           :before_add, :after_add, :before_remove, :after_remove,
@@ -1658,7 +1662,7 @@ def construct_finder_sql_with_included_associations(options, join_dependency)
1658 1662
           add_conditions!(sql, options[:conditions], scope)
1659 1663
           add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
1660 1664
 
1661  
-          add_group!(sql, options[:group], scope)
  1665
+          add_group!(sql, options[:group], options[:having], scope)
1662 1666
           add_order!(sql, options[:order], scope)
1663 1667
           add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections)
1664 1668
           add_lock!(sql, options, scope)
@@ -1714,7 +1718,7 @@ def construct_finder_sql_for_association_limiting(options, join_dependency)
1714 1718
           end
1715 1719
 
1716 1720
           add_conditions!(sql, options[:conditions], scope)
1717  
-          add_group!(sql, options[:group], scope)
  1721
+          add_group!(sql, options[:group], options[:having], scope)
1718 1722
 
1719 1723
           if order && is_distinct
1720 1724
             connection.add_order_by_for_association_limiting!(sql, :order => order)
1  activerecord/lib/active_record/associations/association_proxy.rb
@@ -188,6 +188,7 @@ def set_belongs_to_association_for(record)
188 188
         def merge_options_from_reflection!(options)
189 189
           options.reverse_merge!(
190 190
             :group   => @reflection.options[:group],
  191
+            :having  => @reflection.options[:having],
191 192
             :limit   => @reflection.options[:limit],
192 193
             :offset  => @reflection.options[:offset],
193 194
             :joins   => @reflection.options[:joins],
9  activerecord/lib/active_record/base.rb
@@ -521,6 +521,7 @@ class << self # Class methods
521 521
       # * <tt>:conditions</tt> - An SQL fragment like "administrator = 1", <tt>[ "user_name = ?", username ]</tt>, or <tt>["user_name = :user_name", { :user_name => user_name }]</tt>. See conditions in the intro.
522 522
       # * <tt>:order</tt> - An SQL fragment like "created_at DESC, name".
523 523
       # * <tt>:group</tt> - An attribute name by which the result should be grouped. Uses the <tt>GROUP BY</tt> SQL-clause.
  524
+      # * <tt>:having</tt> - Combined with +:group+ this can be used to filter the records that a <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause.
524 525
       # * <tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned.
525 526
       # * <tt>:offset</tt> - An integer determining the offset from where the rows should be fetched. So at 5, it would skip rows 0 through 4.
526 527
       # * <tt>:joins</tt> - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed)
@@ -1632,7 +1633,7 @@ def construct_finder_sql(options)
1632 1633
           add_joins!(sql, options[:joins], scope)
1633 1634
           add_conditions!(sql, options[:conditions], scope)
1634 1635
 
1635  
-          add_group!(sql, options[:group], scope)
  1636
+          add_group!(sql, options[:group], options[:having], scope)
1636 1637
           add_order!(sql, options[:order], scope)
1637 1638
           add_limit!(sql, options, scope)
1638 1639
           add_lock!(sql, options, scope)
@@ -1688,13 +1689,15 @@ def add_order!(sql, order, scope = :auto)
1688 1689
           end
1689 1690
         end
1690 1691
 
1691  
-        def add_group!(sql, group, scope = :auto)
  1692
+        def add_group!(sql, group, having, scope = :auto)
1692 1693
           if group
1693 1694
             sql << " GROUP BY #{group}"
  1695
+            sql << " HAVING #{having}" if having
1694 1696
           else
1695 1697
             scope = scope(:find) if :auto == scope
1696 1698
             if scope && (scoped_group = scope[:group])
1697 1699
               sql << " GROUP BY #{scoped_group}"
  1700
+              sql << " HAVING #{scoped_having}" if (scoped_having = scope[:having])
1698 1701
             end
1699 1702
           end
1700 1703
         end
@@ -2259,7 +2262,7 @@ def raise_if_bind_arity_mismatch(statement, expected, provided) #:nodoc:
2259 2262
         end
2260 2263
 
2261 2264
         VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset,
2262  
-                               :order, :select, :readonly, :group, :from, :lock ]
  2265
+                               :order, :select, :readonly, :group, :having, :from, :lock ]
2263 2266
 
2264 2267
         def validate_find_options(options) #:nodoc:
2265 2268
           options.assert_valid_keys(VALID_FIND_OPTIONS)
5  activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -658,6 +658,11 @@ def test_find_scoped_grouped
658 658
     assert_equal 1, categories(:technology).posts_gruoped_by_title.size
659 659
   end
660 660
 
  661
+  def test_find_scoped_grouped_having
  662
+    assert_equal 2, projects(:active_record).well_payed_salary_groups.size
  663
+    assert projects(:active_record).well_payed_salary_groups.all? { |g| g.salary > 10000 }
  664
+  end
  665
+
661 666
   def test_get_ids
662 667
     assert_equal projects(:active_record, :action_controller).map(&:id).sort, developers(:david).project_ids.sort
663 668
     assert_equal [projects(:active_record).id], developers(:jamis).project_ids
5  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -255,6 +255,11 @@ def test_find_scoped_grouped
255 255
     assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
256 256
   end
257 257
 
  258
+  def test_find_scoped_grouped_having
  259
+    assert_equal 1, authors(:david).popular_grouped_posts.length
  260
+    assert_equal 0, authors(:mary).popular_grouped_posts.length
  261
+  end
  262
+
258 263
   def test_adding
259 264
     force_signal37_to_load_all_clients_of_firm
260 265
     natural = Client.new("name" => "Natural Company")
7  activerecord/test/cases/finder_test.rb
@@ -175,6 +175,13 @@ def test_find_with_group
175 175
     assert_equal 4, developers.map(&:salary).uniq.size
176 176
   end
177 177
 
  178
+  def test_find_with_group_and_having
  179
+    developers =  Developer.find(:all, :group => "salary", :having => "sum(salary) >  10000", :select => "salary")
  180
+    assert_equal 3, developers.size
  181
+    assert_equal 3, developers.map(&:salary).uniq.size
  182
+    assert developers.all? { |developer|  developer.salary > 10000 }
  183
+  end
  184
+
178 185
   def test_find_with_entire_select_statement
179 186
     topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
180 187
 
1  activerecord/test/models/author.rb
... ...
@@ -1,6 +1,7 @@
1 1
 class Author < ActiveRecord::Base
2 2
   has_many :posts
3 3
   has_many :posts_with_comments, :include => :comments, :class_name => "Post"
  4
+  has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type"
4 5
   has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id'
5 6
   has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1
6 7
   has_many :posts_with_categories, :include => :categories, :class_name => "Post"
1  activerecord/test/models/category.rb
@@ -14,6 +14,7 @@ class Category < ActiveRecord::Base
14 14
                           :class_name => 'Post',
15 15
                           :conditions => { :title => 'Yet Another Testing Title' }
16 16
 
  17
+  has_and_belongs_to_many :popular_grouped_posts, :class_name => "Post", :group => "posts.type", :having => "sum(comments.post_id) > 2", :include => :comments
17 18
   has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title", :select => "title"
18 19
 
19 20
   def self.what_are_you
1  activerecord/test/models/project.rb
@@ -13,6 +13,7 @@ class Project < ActiveRecord::Base
13 13
                             :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"},
14 14
                             :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"},
15 15
                             :after_remove => Proc.new {|o, r| o.developers_log << "after_removing#{r.id}"}
  16
+  has_and_belongs_to_many :well_payed_salary_groups, :class_name => "Developer", :group => "salary", :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary"
16 17
 
17 18
   attr_accessor :developers_log
18 19
 

0 notes on commit 97403ad

Please sign in to comment.
Something went wrong with that request. Please try again.