diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-26 18:14:16 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-26 18:15:09 +0100 |
commit | b3407c86cfccd2cc6258b8879e91600fe25c6e48 (patch) | |
tree | 0dce9206faba6e2df06245850b0bf46c96bca56c /activerecord | |
parent | e97b6f2ef747f013f8d71d7d97dc88acc673ee6d (diff) | |
download | rails-b3407c86cfccd2cc6258b8879e91600fe25c6e48.tar.gz rails-b3407c86cfccd2cc6258b8879e91600fe25c6e48.tar.bz2 rails-b3407c86cfccd2cc6258b8879e91600fe25c6e48.zip |
Don't require a DB connection when setting primary key.
Closes #2807.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/primary_key.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/base.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/primary_keys_test.rb | 16 |
3 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index ed71b5e7d4..a404a5edd7 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -66,7 +66,6 @@ module ActiveRecord @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 diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 558b341c06..78159d13d4 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -707,6 +707,10 @@ module ActiveRecord #:nodoc: # 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 diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 05a41d8a0a..489c7d8310 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -145,4 +145,20 @@ class PrimaryKeysTest < ActiveRecord::TestCase 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 |