From 16f96238ba59fd81017b1402eb665e55c5dd4ba5 Mon Sep 17 00:00:00 2001 From: Kuldeep Aggarwal Date: Sat, 1 Mar 2014 00:51:35 +0530 Subject: [ci skip] correct select examples and doc, ref [522c0fd] --- activerecord/lib/active_record/relation/query_methods.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (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 5d38f0dce8..d88858611c 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -202,7 +202,7 @@ module ActiveRecord # fields are retrieved: # # Model.select(:field) - # # => [#] + # # => [#] # # Although in the above example it looks as though this method returns an # array, it actually returns a relation object and can have other query @@ -211,12 +211,12 @@ module ActiveRecord # The argument to the method can also be an array of fields. # # 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: # @@ -224,7 +224,7 @@ module ActiveRecord # # => "value" # # Accessing attributes of an object that do not have fields retrieved by a select - # will throw ActiveModel::MissingAttributeError: + # except +id+ will throw ActiveModel::MissingAttributeError: # # Model.select(:field).first.other_field # # => ActiveModel::MissingAttributeError: missing attribute: other_field -- cgit v1.2.3 From f317cc8bc007978d7b135ddd1acdd7e3d1e582a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Mon, 3 Mar 2014 17:44:55 +0100 Subject: Make exists? use bound values. When we build a query with an inline value that is a numeric (e.g. because it's out of range for an int4) PostgreSQL doesn't use an index on the column, since it's now comparing numerics and not int4s. This leads to a _very_ slow query. When we use bound parameters instead of inline values PostgreSQL raises numeric_value_out_of_range since no automatic coercion happens. --- activerecord/lib/active_record/relation/finder_methods.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 7099bdd285..1ba7fc47c0 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -292,7 +292,12 @@ module ActiveRecord when Array, Hash relation = relation.where(conditions) else - relation = relation.where(table[primary_key].eq(conditions)) if conditions != :none + if conditions != :none + column = columns_hash[primary_key] + substitute = connection.substitute_at(column, bind_values.length) + relation = where(table[primary_key].eq(substitute)) + relation.bind_values += [[column, conditions]] + end end connection.select_value(relation, "#{name} Exists", relation.bind_values) ? true : false -- cgit v1.2.3 From acbd7ab22e5d9179487bd98110234c54535036c4 Mon Sep 17 00:00:00 2001 From: Marcelo Casiraghi Date: Thu, 23 May 2013 00:17:15 -0300 Subject: Allow string hash values on AR order method This behavior has almost no performance impact: String not allowed 66.910000 0.030000 66.940000 ( 67.024976) String allowed 69.360000 0.030000 69.390000 ( 69.503096) Benchmarked with http://git.io/Y0YuRw. --- activerecord/lib/active_record/relation/query_methods.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (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 d88858611c..a22849f4a6 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1030,10 +1030,13 @@ module ActiveRecord arel.order(*orders) unless orders.empty? end + VALID_DIRECTIONS = [:asc, :desc, 'asc', 'desc'] # :nodoc: + def validate_order_args(args) args.grep(Hash) do |h| - unless (h.values - [:asc, :desc]).empty? - raise ArgumentError, 'Direction should be :asc or :desc' + h.values.map(&:downcase).each do |value| + raise ArgumentError, "Direction '#{value}' is invalid. Valid " \ + "directions are asc and desc." unless VALID_DIRECTIONS.include?(value) end end end @@ -1055,7 +1058,7 @@ module ActiveRecord when Hash arg.map { |field, dir| field = klass.attribute_alias(field) if klass.attribute_alias?(field) - table[field].send(dir) + table[field].send(dir.downcase) } else arg -- cgit v1.2.3 From f6aeb8b1a3687c8523e4a56309fe3736011b2935 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 5 Mar 2014 10:24:13 +0100 Subject: we only need to support `asc` and `ASC`. No need for mixed cases. #14263 This is a result of the discussion at https://github.com/rails/rails/pull/14263/files#r10291489 --- .../lib/active_record/relation/query_methods.rb | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) (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 a22849f4a6..f694a901f4 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -275,15 +275,6 @@ module ActiveRecord # Allows to specify an order attribute: # - # User.order('name') - # => SELECT "users".* FROM "users" ORDER BY name - # - # User.order('name DESC') - # => SELECT "users".* FROM "users" ORDER BY name DESC - # - # User.order('name DESC, email') - # => SELECT "users".* FROM "users" ORDER BY name DESC, email - # # User.order(:name) # => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC # @@ -292,6 +283,15 @@ module ActiveRecord # # User.order(:name, email: :desc) # => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC, "users"."email" DESC + # + # User.order('name') + # => SELECT "users".* FROM "users" ORDER BY name + # + # User.order('name DESC') + # => SELECT "users".* FROM "users" ORDER BY name DESC + # + # User.order('name DESC, email') + # => SELECT "users".* FROM "users" ORDER BY name DESC, email def order(*args) check_if_method_has_arguments!(:order, args) spawn.order!(*args) @@ -1030,13 +1030,14 @@ module ActiveRecord arel.order(*orders) unless orders.empty? end - VALID_DIRECTIONS = [:asc, :desc, 'asc', 'desc'] # :nodoc: + VALID_DIRECTIONS = [:asc, :desc, :ASC, :DESC, + 'asc', 'desc', 'ASC', 'DESC'] # :nodoc: def validate_order_args(args) args.grep(Hash) do |h| - h.values.map(&:downcase).each do |value| - raise ArgumentError, "Direction '#{value}' is invalid. Valid " \ - "directions are asc and desc." unless VALID_DIRECTIONS.include?(value) + h.values.each do |value| + raise ArgumentError, "Direction \"#{value}\" is invalid. Valid " \ + "directions are: #{VALID_DIRECTIONS.inspect}" unless VALID_DIRECTIONS.include?(value) end end end -- cgit v1.2.3 From b74eed58b228b49c5298cf0df9c6890d0b8a4ab5 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 5 Mar 2014 10:45:37 +0100 Subject: get rid of intermediate arrays. origin: https://github.com/rails/rails/commit/f6aeb8b1a3687c8523e4a56309fe3736011b2935#commitcomment-5569649 --- activerecord/lib/active_record/relation/query_methods.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (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 f694a901f4..8c005a7222 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1034,8 +1034,9 @@ module ActiveRecord 'asc', 'desc', 'ASC', 'DESC'] # :nodoc: def validate_order_args(args) - args.grep(Hash) do |h| - h.values.each do |value| + args.each do |arg| + next unless arg.is_a?(Hash) + arg.each do |_key, value| raise ArgumentError, "Direction \"#{value}\" is invalid. Valid " \ "directions are: #{VALID_DIRECTIONS.inspect}" unless VALID_DIRECTIONS.include?(value) end -- cgit v1.2.3