From 9887440a0c0e69afbcd6ccba4243033fb153c8cb Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Mon, 16 Jul 2012 22:45:33 -0700 Subject: Don't link to edgeguides in docs --- activerecord/lib/active_record/relation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index dd1f77e925..05bf3d954c 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -145,7 +145,7 @@ module ActiveRecord # are needed by the next ones when eager loading is going on. # # Please see further details in the - # {Active Record Query Interface guide}[http://edgeguides.rubyonrails.org/active_record_querying.html#running-explain]. + # {Active Record Query Interface guide}[http://guides.rubyonrails.org/active_record_querying.html#running-explain]. def explain _, queries = collecting_queries_for_explain { exec_queries } exec_explain(queries) -- cgit v1.2.3 From 5edbeb0a87f89f1a51c71afbf248e7fa57891b3c Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Mon, 16 Jul 2012 23:17:22 -0700 Subject: Add docs for Relation initialize, create and create! --- activerecord/lib/active_record/relation.rb | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 05bf3d954c..999a38011c 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -75,6 +75,18 @@ module ActiveRecord binds) end + # Initializes new record from relation while maintaing the current + # scope. + # + # Expects arguments in the same format as +Base.new+. + # + # users = User.where(name: 'DHH') + # user = users.new # => # + # + # You can also pass a block to new with the new record as argument: + # + # user = users.new { |user| user.name = 'Oscar' } + # user.name # => Oscar def new(*args, &block) scoping { @klass.new(*args, &block) } end @@ -87,17 +99,38 @@ module ActiveRecord alias build new + # Tries to +create+ a new record with the same scoped attributes + # defined in the relation. Returns the initialized object if validation fails. + # + # Expects arguments in the same format as +Base.create+. + # + # ==== Examples + # users = User.where(name: 'Oscar') + # users.create # # + # + # users.create(name: 'fxn') + # users.create # # + # + # users.create { |user| user.name = 'tenderlove' } + # # # + # + # users.create(name: nil) # validation on name + # # # def create(*args, &block) scoping { @klass.create(*args, &block) } end + # Similar to #create, but calls +create!+ on the base class. Raises + # an exception if a validation error occurs. + # + # Expects arguments in the same format as Base.create!. def create!(*args, &block) scoping { @klass.create!(*args, &block) } end # Tries to load the first record; if it fails, then create is called with the same arguments as this method. # - # Expects arguments in the same format as Base.create. + # Expects arguments in the same format as +Base.create+. # # ==== Examples # # Find the first user named Penélope or create a new one. -- cgit v1.2.3 From 2c050e1808b996e6a0ff2e26981087d8c080fc48 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Tue, 17 Jul 2012 15:48:03 -0700 Subject: Improve docs for AR Relation --- activerecord/lib/active_record/relation.rb | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 999a38011c..6f8f3259a4 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -184,6 +184,7 @@ module ActiveRecord exec_explain(queries) end + # Converts relation objects to Array. def to_a # We monitor here the entire execution rather than individual SELECTs # because from the point of view of the user fetching the records of a @@ -242,6 +243,7 @@ module ActiveRecord c.respond_to?(:zero?) ? c.zero? : c.empty? end + # Returns true if there are any records. def any? if block_given? to_a.any? { |*block_args| yield(*block_args) } @@ -250,6 +252,7 @@ module ActiveRecord end end + # Returns true if there are more than one records. def many? if block_given? to_a.many? { |*block_args| yield(*block_args) } @@ -283,17 +286,14 @@ module ActiveRecord # ==== Parameters # # * +updates+ - A string, array, or hash representing the SET part of an SQL statement. - # * +conditions+ - A string, array, or hash representing the WHERE part of an SQL statement. - # See conditions in the intro. - # * +options+ - Additional options are :limit and :order, see the examples for usage. # # ==== Examples # # # Update all customers with the given attributes - # Customer.update_all :wants_email => true + # Customer.update_all wants_email: true # # # Update all books with 'Rails' in their title - # Book.where('title LIKE ?', '%Rails%').update_all(:author => 'David') + # Book.where('title LIKE ?', '%Rails%').update_all(author: 'David') # # # Update all books that match conditions, but limit it to 5 ordered by date # Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David') @@ -326,7 +326,7 @@ module ActiveRecord # ==== Examples # # # Updates one record - # Person.update(15, :user_name => 'Samuel', :group => 'expert') + # Person.update(15, user_name: 'Samuel', group: 'expert') # # # Updates multiple records # people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } } @@ -366,7 +366,7 @@ module ActiveRecord # ==== Examples # # Person.destroy_all("last_login < '2004-04-04'") - # Person.destroy_all(:status => "inactive") + # Person.destroy_all(status: "inactive") # Person.where(:age => 0..18).destroy_all def destroy_all(conditions = nil) if conditions @@ -468,6 +468,7 @@ module ActiveRecord where(primary_key => id_or_array).delete_all end + # Forces reloading of relation. def reload reset to_a # force reload @@ -481,10 +482,18 @@ module ActiveRecord self end + # Returns sql statement for the relation. + # + # Users.where(name: 'Oscar').to_sql + # # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar' def to_sql @to_sql ||= klass.connection.to_sql(arel, bind_values.dup) end + # Returns an hash of where conditions + # + # Users.where(name: 'Oscar').to_sql + # # => {:name=>"oscar"} def where_values_hash equalities = with_default_scope.where_values.grep(Arel::Nodes::Equality).find_all { |node| node.left.relation.name == table_name @@ -502,6 +511,7 @@ module ActiveRecord @scope_for_create ||= where_values_hash.merge(create_with_value) end + # Returns true if relation needs eager loading. def eager_loading? @should_eager_load ||= eager_load_values.any? || @@ -516,6 +526,7 @@ module ActiveRecord includes_values & joins_values end + # Compares two relations for equality. def ==(other) case other when Relation @@ -539,6 +550,7 @@ module ActiveRecord end end + # Returns true if relation is blank> def blank? to_a.blank? end -- cgit v1.2.3 From 30bf42b3ef8be104abffba07c3feaa343d882e09 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Tue, 17 Jul 2012 15:53:36 -0700 Subject: Fix typos and add nodocs to NullRelation --- activerecord/lib/active_record/relation.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 6f8f3259a4..cc03ebf5d6 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -490,9 +490,9 @@ module ActiveRecord @to_sql ||= klass.connection.to_sql(arel, bind_values.dup) end - # Returns an hash of where conditions + # Returns a hash of where conditions # - # Users.where(name: 'Oscar').to_sql + # Users.where(name: 'Oscar').where_values_hash # # => {:name=>"oscar"} def where_values_hash equalities = with_default_scope.where_values.grep(Arel::Nodes::Equality).find_all { |node| @@ -550,7 +550,7 @@ module ActiveRecord end end - # Returns true if relation is blank> + # Returns true if relation is blank. def blank? to_a.blank? end -- cgit v1.2.3 From 11d488dd7a5adf1c5c5044ee2cdd96e127e67cd4 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Thu, 19 Jul 2012 11:16:02 -0700 Subject: Fix typos --- activerecord/lib/active_record/relation.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index cc03ebf5d6..3095580b66 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -75,7 +75,7 @@ module ActiveRecord binds) end - # Initializes new record from relation while maintaing the current + # Initializes new record from relation while maintaining the current # scope. # # Expects arguments in the same format as +Base.new+. @@ -99,7 +99,7 @@ module ActiveRecord alias build new - # Tries to +create+ a new record with the same scoped attributes + # Tries to create a new record with the same scoped attributes # defined in the relation. Returns the initialized object if validation fails. # # Expects arguments in the same format as +Base.create+. @@ -252,7 +252,7 @@ module ActiveRecord end end - # Returns true if there are more than one records. + # Returns true if there is more than one records. def many? if block_given? to_a.many? { |*block_args| yield(*block_args) } -- cgit v1.2.3 From c851deff83076b8f73c77818bc04ba8e9f7ec335 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sat, 21 Jul 2012 21:44:16 +0530 Subject: minor copy edits [ci skip] --- activerecord/lib/active_record/relation.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 3095580b66..3821c6122a 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -252,7 +252,7 @@ module ActiveRecord end end - # Returns true if there is more than one records. + # Returns true if there is more than one record. def many? if block_given? to_a.many? { |*block_args| yield(*block_args) } @@ -263,8 +263,6 @@ module ActiveRecord # Scope all queries to the current scope. # - # ==== Example - # # Comment.where(:post_id => 1).scoping do # Comment.first # SELECT * FROM comments WHERE post_id = 1 # end -- cgit v1.2.3