Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Quote find_in_batches ORDER BY clause [#6620 state:resolved]

  • Loading branch information...
commit 3ed8a331973b80c49d56b65d726b1f8be52e5261 1 parent 7f098a6
Andrew White pixeltrix authored
6 activerecord/lib/active_record/attribute_methods/primary_key.rb
View
@@ -16,6 +16,11 @@ def primary_key
reset_primary_key
end
+ # Returns a quoted version of the primary key name, used to construct SQL statements.
+ def quoted_primary_key
+ @quoted_primary_key ||= connection.quote_column_name(primary_key)
+ end
+
def reset_primary_key #:nodoc:
key = get_primary_key(base_class.name)
set_primary_key(key)
@@ -41,6 +46,7 @@ def get_primary_key(base_name) #:nodoc:
# set_primary_key "sysid"
# end
def set_primary_key(value = nil, &block)
+ @quoted_primary_key = nil
define_attr_method :primary_key, value, &block
end
alias :primary_key= :set_primary_key
2  activerecord/lib/active_record/relation/batches.rb
View
@@ -83,7 +83,7 @@ def find_in_batches(options = {})
private
def batch_order
- "#{@klass.table_name}.#{@klass.primary_key} ASC"
+ "#{@klass.quoted_table_name}.#{@klass.quoted_primary_key} ASC"
end
end
end
10 activerecord/test/cases/batches_test.rb
View
@@ -82,4 +82,14 @@ def test_find_in_batches_shouldnt_excute_query_unless_needed
Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch }
end
end
+
+ def test_find_in_batches_should_quote_batch_order
+ c = Post.connection
+ assert_sql(/ORDER BY #{c.quote_table_name('posts')}.#{c.quote_column_name('id')}/) do
+ Post.find_in_batches(:batch_size => 1) do |batch|
+ assert_kind_of Array, batch
+ assert_kind_of Post, batch.first
+ end
+ end
+ end
end
9 activerecord/test/cases/primary_keys_test.rb
View
@@ -136,4 +136,13 @@ def test_primary_key_returns_nil_if_it_does_not_exist
assert_nil ActiveRecord::Base.connection.primary_key('developers_projects')
end
end
+
+ def test_quoted_primary_key_after_set_primary_key
+ k = Class.new( ActiveRecord::Base )
+ assert_equal k.connection.quote_column_name("id"), k.quoted_primary_key
+ k.primary_key = "foo"
+ assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key
+ k.set_primary_key "bar"
+ assert_equal k.connection.quote_column_name("bar"), k.quoted_primary_key
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.