Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

If the table behind has no primary key, do not ask again and just ret…

…urn nil.

Conflicts:

	activerecord/test/cases/attribute_methods_test.rb
  • Loading branch information...
commit 5f27545ef5d3c4fa55b5470fce27e5b0e77f2c57 1 parent c6e513b
@pimeys pimeys authored jonleighton committed
View
6 activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+## Rails 3.1.4 (unreleased) ##
+
+* Perf fix: If a table has no primary key, don't repeatedly ask the database for it.
+
+ *Julius de Bruijn*
+
## Rails 3.1.3 (unreleased) ##
* Perf fix: If we're deleting all records in an association, don't add a IN(..) clause
View
3  activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -13,7 +13,8 @@ module ClassMethods
# Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
# primary_key_prefix_type setting, though.
def primary_key
- @primary_key ||= reset_primary_key
+ @primary_key = reset_primary_key unless defined? @primary_key
+ @primary_key
end
# Returns a quoted version of the primary key name, used to construct SQL statements.
View
5 activerecord/test/cases/attribute_methods_test.rb
@@ -35,6 +35,11 @@ def test_attribute_present
assert !t.attribute_present?("content")
end
+ def test_caching_nil_primary_key
+ Minimalistic.expects(:reset_primary_key).returns(nil).once
+ Minimalistic.create!
+ end
+
def test_attribute_keys_on_new_instance
t = Topic.new
assert_equal nil, t.title, "The topics table has a title column, so it should be nil"

0 comments on commit 5f27545

Please sign in to comment.
Something went wrong with that request. Please try again.