From 2120da7f733ba33183a42e71256db9652c5f5fcc Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 3 Jan 2011 22:47:07 +0000 Subject: ActiveRecord::Relation#primary_key should return a string, just like ActiveRecord::Base.primary_key does. --- activerecord/lib/active_record/nested_attributes.rb | 2 +- activerecord/lib/active_record/relation.rb | 19 ++++++------------- activerecord/lib/active_record/relation/batches.rb | 6 +++--- .../lib/active_record/relation/calculations.rb | 2 +- .../lib/active_record/relation/finder_methods.rb | 18 +++++++++--------- .../lib/active_record/relation/query_methods.rb | 2 +- activerecord/test/cases/relations_test.rb | 4 ++++ 7 files changed, 25 insertions(+), 28 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index f3a32e85e6..16023defe3 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -393,7 +393,7 @@ module ActiveRecord association.to_a else attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact - attribute_ids.empty? ? [] : association.all(:conditions => {association.klass.primary_key => attribute_ids}) + attribute_ids.empty? ? [] : association.all(:conditions => {association.primary_key => attribute_ids}) end attributes_collection.each do |attributes| diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 20e983b5f7..1441e9750e 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -10,7 +10,9 @@ module ActiveRecord include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches + # These are explicitly delegated to improve performance (avoids method_missing) delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to => :to_a + delegate :table_name, :primary_key, :to => :klass attr_reader :table, :klass, :loaded attr_accessor :extensions @@ -30,13 +32,12 @@ module ActiveRecord def insert(values) im = arel.compile_insert values im.into @table - primary_key_name = @klass.primary_key - primary_key_value = primary_key_name && Hash === values ? values[primary_key] : nil + primary_key_value = primary_key && Hash === values ? values[table[primary_key]] : nil @klass.connection.insert( im.to_sql, 'SQL', - primary_key_name, + primary_key, primary_key_value) end @@ -177,7 +178,7 @@ module ActiveRecord stmt = arel.compile_update(Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))) stmt.take limit stmt.order(*order) - stmt.key = @klass.arel_table[@klass.primary_key] + stmt.key = table[primary_key] @klass.connection.update stmt.to_sql end end @@ -320,7 +321,7 @@ module ActiveRecord # # Delete multiple rows # Todo.delete([2,3,4]) def delete(id_or_array) - where(@klass.primary_key => id_or_array).delete_all + where(primary_key => id_or_array).delete_all end def reload @@ -336,10 +337,6 @@ module ActiveRecord self end - def primary_key - @primary_key ||= table[@klass.primary_key] - end - def to_sql @to_sql ||= arel.to_sql end @@ -373,10 +370,6 @@ module ActiveRecord to_a.inspect end - def table_name - @klass.table_name - end - protected def method_missing(method, *args, &block) diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index b41e935ed5..359af9820f 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -65,7 +65,7 @@ module ActiveRecord batch_size = options.delete(:batch_size) || 1000 relation = relation.except(:order).order(batch_order).limit(batch_size) - records = relation.where(primary_key.gteq(start)).all + records = relation.where(table[primary_key].gteq(start)).all while records.any? yield records @@ -73,7 +73,7 @@ module ActiveRecord break if records.size < batch_size if primary_key_offset = records.last.id - records = relation.where(primary_key.gt(primary_key_offset)).to_a + records = relation.where(table[primary_key].gt(primary_key_offset)).to_a else raise "Primary key not included in the custom select clause" end @@ -83,7 +83,7 @@ module ActiveRecord private def batch_order - "#{@klass.table_name}.#{@klass.primary_key} ASC" + "#{table_name}.#{primary_key} ASC" end end end diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 139752b190..e9e451ec5c 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -168,7 +168,7 @@ module ActiveRecord unless arel.ast.grep(Arel::Nodes::OuterJoin).empty? distinct = true - column_name = @klass.primary_key if column_name == :all + column_name = primary_key if column_name == :all end distinct = nil if column_name =~ /\s*DISTINCT\s+/i diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 8bc28c06cb..13f55319a7 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -171,13 +171,13 @@ module ActiveRecord def exists?(id = nil) id = id.id if ActiveRecord::Base === id - relation = select(primary_key).limit(1) + relation = select(table[primary_key]).limit(1) case id when Array, Hash relation = relation.where(id) else - relation = relation.where(primary_key.eq(id)) if id + relation = relation.where(table[primary_key].eq(id)) if id end relation.first ? true : false @@ -225,10 +225,10 @@ module ActiveRecord def construct_limited_ids_condition(relation) orders = relation.order_values - values = @klass.connection.distinct("#{@klass.connection.quote_table_name @klass.table_name}.#{@klass.primary_key}", orders) + values = @klass.connection.distinct("#{@klass.connection.quote_table_name table_name}.#{primary_key}", orders) - ids_array = relation.select(values).collect {|row| row[@klass.primary_key]} - ids_array.empty? ? raise(ThrowResult) : primary_key.in(ids_array) + ids_array = relation.select(values).collect {|row| row[primary_key]} + ids_array.empty? ? raise(ThrowResult) : table[primary_key].in(ids_array) end def find_by_attributes(match, attributes, *args) @@ -290,24 +290,24 @@ module ActiveRecord def find_one(id) id = id.id if ActiveRecord::Base === id - column = columns_hash[primary_key.name.to_s] + column = columns_hash[primary_key] substitute = connection.substitute_for(column, @bind_values) - relation = where(primary_key.eq(substitute)) + relation = where(table[primary_key].eq(substitute)) relation.bind_values = [[column, id]] record = relation.first unless record conditions = arel.where_sql conditions = " [#{conditions}]" if conditions - raise RecordNotFound, "Couldn't find #{@klass.name} with #{@klass.primary_key}=#{id}#{conditions}" + raise RecordNotFound, "Couldn't find #{@klass.name} with #{primary_key}=#{id}#{conditions}" end record end def find_some(ids) - result = where(primary_key.in(ids)).all + result = where(table[primary_key].in(ids)).all expected_size = if @limit_value && ids.size > @limit_value diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index df09226977..2cbb103eb9 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -152,7 +152,7 @@ module ActiveRecord order_clause = arel.order_clauses order = order_clause.empty? ? - "#{@klass.table_name}.#{@klass.primary_key} DESC" : + "#{table_name}.#{primary_key} DESC" : reverse_sql_order(order_clause).join(', ') except(:order).order(Arel.sql(order)) diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 4de180880c..5018b16b67 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -810,4 +810,8 @@ class RelationTest < ActiveRecord::TestCase def test_removing_limit_with_options assert_not_equal 1, Post.limit(1).all(:limit => nil).count end + + def test_primary_key + assert_equal "id", Post.scoped.primary_key + end end -- cgit v1.2.3