mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
r2726@asus: jeremy | 2005-10-24 14:11:59 -0700
correct primary key semantics: foo.id and foo.real_pk_column should both exist and return nil if foo.new_record? git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2718 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
8ac9638646
commit
0e4c5b0288
2 changed files with 6 additions and 7 deletions
|
@ -1451,7 +1451,7 @@ module ActiveRecord #:nodoc:
|
|||
cast_code = column.type_cast_code('v')
|
||||
access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
|
||||
|
||||
unless symbol == :id
|
||||
unless attr_name.to_s == self.class.primary_key.to_s
|
||||
access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'abstract_unit'
|
||||
require "#{File.dirname(__FILE__)}/abstract_unit"
|
||||
require 'fixtures/topic'
|
||||
require 'fixtures/subscriber'
|
||||
require 'fixtures/movie'
|
||||
|
@ -16,7 +16,7 @@ class PrimaryKeysTest < Test::Unit::TestCase
|
|||
topic = Topic.new
|
||||
topic.title = "New Topic"
|
||||
assert_equal(nil, topic.id)
|
||||
assert_nothing_raised{ topic.save }
|
||||
assert_nothing_raised { topic.save! }
|
||||
id = topic.id
|
||||
|
||||
topicReloaded = Topic.find(id)
|
||||
|
@ -25,15 +25,14 @@ class PrimaryKeysTest < Test::Unit::TestCase
|
|||
|
||||
def test_customized_primary_key_auto_assigns_on_save
|
||||
keyboard = Keyboard.new(:name => 'HHKB')
|
||||
assert_nothing_raised { keyboard.save }
|
||||
assert keyboard.id
|
||||
assert_nothing_raised { keyboard.save! }
|
||||
assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
|
||||
end
|
||||
|
||||
def test_customized_primary_key_can_be_get_before_saving
|
||||
keyboard = Keyboard.new
|
||||
assert_respond_to(keyboard, :key_number)
|
||||
assert_nothing_raised { keyboard.key_number }
|
||||
assert_nil keyboard.id
|
||||
assert_nothing_raised { assert_nil keyboard.key_number }
|
||||
end
|
||||
|
||||
def test_customized_string_primary_key_settable_before_save
|
||||
|
|
Loading…
Reference in a new issue