If the table behind has no primary key, do not ask again and just return nil.

This commit is contained in:
Julius de Bruijn
2011-11-30 17:56:16 +01:00
committed by Jon Leighton
parent 7a4949e7d5
commit 4e380828ff
2 changed files with 7 additions and 1 deletions

View File

@@ -33,7 +33,8 @@ module ActiveRecord
# 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 File

@@ -53,6 +53,11 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert Boolean.find(b4.id).attribute_present?(:value)
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"