Permalink
Browse files

Don't require a DB connection when setting primary key.

Closes #2807.
  • Loading branch information...
1 parent 493077c commit 75e97a4888622d20f4e8d0251ad9a02fbfcd146b @jonleighton jonleighton committed Sep 26, 2011
@@ -1,5 +1,9 @@
*Rails 3.1.1 (unreleased)*
+* Don't require a DB connection when loading a model that uses set_primary_key. GH #2807.
+
+ [Jon Leighton]
+
* Fix using select() with a habtm association, e.g. Person.friends.select(:name). GH #3030 and
#2923.
@@ -66,7 +66,6 @@ def set_primary_key(value = nil, &block)
@primary_key ||= ''
self.original_primary_key = @primary_key
value &&= value.to_s
- connection_pool.primary_keys[table_name] = value
self.primary_key = block_given? ? instance_eval(&block) : value
end
end
@@ -705,6 +705,10 @@ def table_exists?
# Returns an array of column objects for the table associated with this class.
def columns
+ if defined?(@primary_key)
+ connection_pool.primary_keys[table_name] ||= primary_key
+ end
+
connection_pool.columns[table_name]
end
@@ -145,4 +145,20 @@ def test_quoted_primary_key_after_set_primary_key
k.set_primary_key "bar"
assert_equal k.connection.quote_column_name("bar"), k.quoted_primary_key
end
+
+ def test_set_primary_key_with_no_connection
+ return skip("disconnect wipes in-memory db") if in_memory_db?
+
+ connection = ActiveRecord::Base.remove_connection
+
+ model = Class.new(ActiveRecord::Base) do
+ set_primary_key 'foo'
+ end
+
+ assert_equal 'foo', model.primary_key
+
+ ActiveRecord::Base.establish_connection(connection)
+
+ assert_equal 'foo', model.primary_key
+ end
end

0 comments on commit 75e97a4

Please sign in to comment.