From a8c2981f62555a7ea7e5b378509fca6009ba8a8e Mon Sep 17 00:00:00 2001 From: Maurizio De Santis Date: Thu, 7 Feb 2013 21:24:45 +0100 Subject: Add ActiveRecord.count documentation when used on group relations --- activerecord/lib/active_record/relation/calculations.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index f10c290755..3fc079d84c 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -13,6 +13,12 @@ module ActiveRecord # # Person.count(:age, distinct: true) # # => counts the number of different age values + # + # If used on a +group+, it returns a Hash whose keys represent the aggregated column, + # and the values are the respective amounts: + # + # Person.group(:city).count + # # => { 'Rome' => 5, 'Paris' => 3 } def count(column_name = nil, options = {}) column_name, options = nil, column_name if column_name.is_a?(Hash) calculate(:count, column_name, options) -- cgit v1.2.3 From ba407c8f7b8ca48d9f20318d23b60b32d2cd6b9c Mon Sep 17 00:00:00 2001 From: Maurizio De Santis Date: Thu, 7 Feb 2013 22:43:50 +0100 Subject: Add documentation for ActiveRecord.select when use strings as arguments --- activerecord/lib/active_record/relation/query_methods.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 42849d6bc9..c025facbb9 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -189,6 +189,16 @@ module ActiveRecord # Model.select(:field, :other_field, :and_one_more) # # => [#] # + # You can also use one or more strings, which will be used unchanged as SELECT fields. + # + # Model.select('field AS field_one', 'other_field AS field_two') + # # => [#] + # + # If an alias was specified, it will be accessible from the resulting objects: + # + # Model.select('field AS field_one').first.field_one + # # => "value" + # # Accessing attributes of an object that do not have fields retrieved by a select # will throw ActiveModel::MissingAttributeError: # -- cgit v1.2.3 From cf5e0f490cf9f9c82b322263dc768176edbdc27f Mon Sep 17 00:00:00 2001 From: Maurizio De Santis Date: Thu, 7 Feb 2013 22:50:17 +0100 Subject: Add example for ActiveRecord::Base.group using a string as argument --- activerecord/lib/active_record/relation/query_methods.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index c025facbb9..4e4e58b319 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -230,6 +230,9 @@ module ActiveRecord # # User.group(:name) # => [#, #] + # + # User.group('name AS grouped_name, age') + # => [#, #, #] def group(*args) args.blank? ? self : spawn.group!(*args) end -- cgit v1.2.3 From 7c663f507f5b4f1f443949c92dbc375d42dbd6b8 Mon Sep 17 00:00:00 2001 From: Maurizio De Santis Date: Thu, 7 Feb 2013 23:01:19 +0100 Subject: Add example for ActiveRecord::Base.joins using a string as argument --- activerecord/lib/active_record/relation/query_methods.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 4e4e58b319..63836bf375 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -305,6 +305,11 @@ module ActiveRecord # # User.joins(:posts) # => SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" + # + # You can use strings in order to customize your joins: + # + # User.joins("LEFT JOIN bookmarks ON bookmarks.bookmarkable_type = 'Post' AND bookmarks.user_id = users.id") + # => SELECT "users".* FROM "users" LEFT JOIN bookmarks ON bookmarks.bookmarkable_type = 'Post' AND bookmarks.user_id = users.id def joins(*args) args.compact.blank? ? self : spawn.joins!(*args.flatten) end -- cgit v1.2.3 From 2008fe606b6096e4b8ad9c03e03b0d11b94605cd Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Fri, 15 Feb 2013 22:01:11 +0530 Subject: copy edits [ci skip] --- activerecord/lib/active_record/relation/calculations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 3fc079d84c..7f95181c67 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -14,7 +14,7 @@ module ActiveRecord # Person.count(:age, distinct: true) # # => counts the number of different age values # - # If used on a +group+, it returns a Hash whose keys represent the aggregated column, + # If +count+ is used with +group+, it returns a Hash whose keys represent the aggregated column, # and the values are the respective amounts: # # Person.group(:city).count -- cgit v1.2.3