Merge pull request #13061 from laurocaetano/fix-uniqueness-validation-for-aliased-attribute
Fix bug when validating the uniqueness of an aliased attribute. Conflicts: activerecord/CHANGELOG.md
This commit is contained in:
parent
f7e4e37ae7
commit
5fdbec7dd1
@ -1,3 +1,9 @@
|
||||
* Fix bug when validating the uniqueness of an aliased attribute.
|
||||
|
||||
Fixes #12402.
|
||||
|
||||
*Lauro Caetano*
|
||||
|
||||
* Update counter cache on a has_many relationship regardless of default scope
|
||||
|
||||
Fix #12952.
|
||||
|
@ -51,7 +51,15 @@ def build_relation(klass, table, attribute, value) #:nodoc:
|
||||
value = value.attributes[reflection.primary_key_column.name] unless value.nil?
|
||||
end
|
||||
|
||||
column = klass.columns_hash[attribute.to_s]
|
||||
attribute_name = attribute.to_s
|
||||
|
||||
# the attribute may be an aliased attribute
|
||||
if klass.attribute_aliases[attribute_name]
|
||||
attribute = klass.attribute_aliases[attribute_name]
|
||||
attribute_name = attribute.to_s
|
||||
end
|
||||
|
||||
column = klass.columns_hash[attribute_name]
|
||||
value = klass.connection.type_cast(value, column)
|
||||
value = value.to_s[0, column.limit] if value && column.limit && column.text?
|
||||
|
||||
|
@ -63,6 +63,14 @@ def test_validate_uniqueness
|
||||
assert t2.save, "Should now save t2 as unique"
|
||||
end
|
||||
|
||||
def test_validate_uniqueness_with_alias_attribute
|
||||
Topic.alias_attribute :new_title, :title
|
||||
Topic.validates_uniqueness_of(:new_title)
|
||||
|
||||
topic = Topic.new(new_title: 'abc')
|
||||
assert topic.valid?
|
||||
end
|
||||
|
||||
def test_validates_uniqueness_with_nil_value
|
||||
Topic.validates_uniqueness_of(:title)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user