diff options
author | Rick Olson <technoweenie@gmail.com> | 2006-10-13 08:52:33 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2006-10-13 08:52:33 +0000 |
commit | 2c6b6e2866c89b8158811c649c5292eda5d24012 (patch) | |
tree | 2914d243ed4bcbc66e5b5a6dbcb4a2d85a587a10 /activerecord | |
parent | e789b26e652d9f0ee6ca538615b8d570cb4c91b5 (diff) | |
download | rails-2c6b6e2866c89b8158811c649c5292eda5d24012.tar.gz rails-2c6b6e2866c89b8158811c649c5292eda5d24012.tar.bz2 rails-2c6b6e2866c89b8158811c649c5292eda5d24012.zip |
automatically add primary key to #select_limited_ids_list order by clause for databases that require order columns in the distinct statements (postgresql) [Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5292 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
4 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index d52695d162..505ebea9f6 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1197,6 +1197,7 @@ module ActiveRecord sql = "SELECT " if is_distinct ordered_columns = options[:order].to_s.split(',').collect! { |s| s.split.first } + options[:order] = "#{table_name}.#{primary_key}, #{options[:order]}" if options[:order] && connection.requires_order_columns_in_distinct_clause? sql << connection.distinct("#{table_name}.#{primary_key}", ordered_columns) else sql << primary_key diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 949b8f7951..b753c248c5 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -56,6 +56,12 @@ module ActiveRecord false end + # Does this adapter require the order columns to be in the select clause + # of a DISTINCT query? This is +false+ in all adapters except postgresql. + def requires_order_columns_in_distinct_clause? + false + end + def reset_runtime #:nodoc: rt, @runtime = @runtime, 0 rt diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 975768a758..e5e098a4e1 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -111,6 +111,10 @@ module ActiveRecord 63 end + def requires_order_columns_in_distinct_clause? + true + end + # QUOTING ================================================== def quote(value, column = nil) diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index a955a29fc1..d033ef087a 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1793,7 +1793,7 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase join_base = ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase.new(Project) join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(join_base, :developers, nil) - projects = Project.send(:select_limited_ids_list, {:order => 'projects.id, developers.created_at'}, join_dep) + projects = Project.send(:select_limited_ids_list, {:order => 'developers.created_at'}, join_dep) assert_equal "'1', '2'", projects end end |