Skip to content
This repository
Browse code

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
Julius de Bruijn authored jonleighton committed
6  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,3 +1,9 @@
  1
+## Rails 3.1.4 (unreleased) ##
  2
+
  3
+*   Perf fix: If a table has no primary key, don't repeatedly ask the database for it.
  4
+
  5
+    *Julius de Bruijn*
  6
+
1 7
 ## Rails 3.1.3 (unreleased) ##
2 8
 
3 9
 *   Perf fix: If we're deleting all records in an association, don't add a IN(..) clause
3  activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -13,7 +13,8 @@ module ClassMethods
13 13
         # Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
14 14
         # primary_key_prefix_type setting, though.
15 15
         def primary_key
16  
-          @primary_key ||= reset_primary_key
  16
+          @primary_key = reset_primary_key unless defined? @primary_key
  17
+          @primary_key
17 18
         end
18 19
 
19 20
         # Returns a quoted version of the primary key name, used to construct SQL statements.
5  activerecord/test/cases/attribute_methods_test.rb
@@ -35,6 +35,11 @@ def test_attribute_present
35 35
     assert !t.attribute_present?("content")
36 36
   end
37 37
 
  38
+  def test_caching_nil_primary_key
  39
+    Minimalistic.expects(:reset_primary_key).returns(nil).once
  40
+    Minimalistic.create!
  41
+  end
  42
+
38 43
   def test_attribute_keys_on_new_instance
39 44
     t = Topic.new
40 45
     assert_equal nil, t.title, "The topics table has a title column, so it should be nil"

0 notes on commit 5f27545

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