aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-11-10 16:36:01 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-11-10 16:36:01 +0000
commit3309268120fade0cd643750a4bedf95097cbced6 (patch)
treea014ca1505d05d4e5833e396efec357f2b7b0ad6 /activerecord
parent3488ff6fc6aa091821088df47ee0ca3ebdd594f0 (diff)
downloadrails-3309268120fade0cd643750a4bedf95097cbced6.tar.gz
rails-3309268120fade0cd643750a4bedf95097cbced6.tar.bz2
rails-3309268120fade0cd643750a4bedf95097cbced6.zip
Add :group option, correspond to GROUP BY, to the find method and to the has_many association. Closes #2818.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2974 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb5
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb3
-rwxr-xr-xactiverecord/lib/active_record/base.rb5
-rwxr-xr-xactiverecord/test/associations_test.rb7
5 files changed, 19 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 33e3dafd14..1bc62c3944 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add :group option, correspond to GROUP BY, to the find method and to the has_many association. #2818 [rubyonrails@atyp.de]
+
* Don't cast nil or empty strings to a dummy date. #2789 [Rick Bradley <rick@rickbradley.com>]
* acts_as_list plays nicely with inheritance by remembering the class which declared it. #2811 [rephorm@rephorm.com]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index a14aed1700..a4c440f4b4 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -307,6 +307,8 @@ module ActiveRecord
# sql fragment, such as "price > 5 AND name LIKE 'B%'".
# * <tt>:order</tt> - specify the order in which the associated objects are returned as a "ORDER BY" sql fragment,
# such as "last_name, first_name DESC"
+ # * <tt>:group</tt> - specify the attribute by which the associated objects are returned as a "GROUP BY" sql fragment,
+ # such as "category"
# * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
# of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_many association will use "person_id"
# as the default foreign_key.
@@ -341,7 +343,8 @@ module ActiveRecord
options.assert_valid_keys(
:foreign_key, :class_name, :exclusively_dependent, :dependent,
:conditions, :order, :include, :finder_sql, :counter_sql,
- :before_add, :after_add, :before_remove, :after_remove, :extend
+ :before_add, :after_add, :before_remove, :after_remove, :extend,
+ :group
)
options[:extend] = create_extension_module(association_id, extension) if block_given?
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index 3027e26ce0..288742d965 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -113,7 +113,8 @@ module ActiveRecord
:order => @options[:order],
:limit => @options[:limit],
:joins => @options[:joins],
- :include => @options[:include]
+ :include => @options[:include],
+ :group => @options[:group]
)
end
end
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 06ac4459de..47b8b6b92b 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -363,6 +363,7 @@ module ActiveRecord #:nodoc:
#
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro.
# * <tt>:order</tt>: An SQL fragment like "created_at DESC, name".
+ # * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
# * <tt>:limit</tt>: An integer determining the limit on the number of rows that should be returned.
# * <tt>:offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
@@ -391,6 +392,7 @@ module ActiveRecord #:nodoc:
# Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)
# Person.find(:all, :offset => 10, :limit => 10)
# Person.find(:all, :include => [ :account, :friends ])
+ # Person.find(:all, :group => "category")
def find(*args)
options = extract_options_from_args!(args)
@@ -926,6 +928,7 @@ module ActiveRecord #:nodoc:
sql = "SELECT #{options[:select] || '*'} FROM #{table_name} "
add_joins!(sql, options)
add_conditions!(sql, options[:conditions])
+ sql << " GROUP BY #{options[:group]} " if options[:group]
sql << " ORDER BY #{options[:order]} " if options[:order]
add_limit!(sql, options)
sql
@@ -1171,7 +1174,7 @@ module ActiveRecord #:nodoc:
end
def validate_find_options(options)
- options.assert_valid_keys [:conditions, :include, :joins, :limit, :offset, :order, :select, :readonly]
+ options.assert_valid_keys [:conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group]
end
def encode_quoted_value(value)
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 4ad40f338c..fd15098f36 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -384,6 +384,13 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
end
+ def test_find_grouped
+ all_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1")
+ grouped_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count')
+ assert_equal 2, all_clients_of_firm1.size
+ assert_equal 1, grouped_clients_of_firm1.size
+ end
+
def test_adding
force_signal37_to_load_all_clients_of_firm
natural = Client.new("name" => "Natural Company")