mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
e97b6f2ef7
commit
b3407c86cf
3 changed files with 20 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue