mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #29464 from eugeneius/raw_write_attribute
Don't map id to primary key in raw_write_attribute
This commit is contained in:
commit
703128dbfe
2 changed files with 12 additions and 15 deletions
|
@ -35,11 +35,15 @@ module ActiveRecord
|
||||||
attr_name.to_s
|
attr_name.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
write_attribute_with_type_cast(name, value, true)
|
name = self.class.primary_key if name == "id".freeze && self.class.primary_key
|
||||||
|
@attributes.write_from_user(name, value)
|
||||||
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
def raw_write_attribute(attr_name, value) # :nodoc:
|
def raw_write_attribute(attr_name, value) # :nodoc:
|
||||||
write_attribute_with_type_cast(attr_name, value, false)
|
name = attr_name.to_s
|
||||||
|
@attributes.write_cast_value(name, value)
|
||||||
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -47,19 +51,6 @@ module ActiveRecord
|
||||||
def attribute=(attribute_name, value)
|
def attribute=(attribute_name, value)
|
||||||
write_attribute(attribute_name, value)
|
write_attribute(attribute_name, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_attribute_with_type_cast(attr_name, value, should_type_cast)
|
|
||||||
attr_name = attr_name.to_s
|
|
||||||
attr_name = self.class.primary_key if attr_name == "id" && self.class.primary_key
|
|
||||||
|
|
||||||
if should_type_cast
|
|
||||||
@attributes.write_from_user(attr_name, value)
|
|
||||||
else
|
|
||||||
@attributes.write_cast_value(attr_name, value)
|
|
||||||
end
|
|
||||||
|
|
||||||
value
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -80,6 +80,12 @@ class PrimaryKeysTest < ActiveRecord::TestCase
|
||||||
assert_equal 1, subscriber.update_count
|
assert_equal 1, subscriber.update_count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_update_columns_with_non_primary_key_id_column
|
||||||
|
subscriber = Subscriber.first
|
||||||
|
subscriber.update_columns(id: 1)
|
||||||
|
assert_not_equal 1, subscriber.nick
|
||||||
|
end
|
||||||
|
|
||||||
def test_string_key
|
def test_string_key
|
||||||
subscriber = Subscriber.find(subscribers(:first).nick)
|
subscriber = Subscriber.find(subscribers(:first).nick)
|
||||||
assert_equal(subscribers(:first).name, subscriber.name)
|
assert_equal(subscribers(:first).name, subscriber.name)
|
||||||
|
|
Loading…
Reference in a new issue