mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
3 changed files with 23 additions and 1 deletions
|
@ -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
|
* Update counter cache on a has_many relationship regardless of default scope
|
||||||
|
|
||||||
Fix #12952.
|
Fix #12952.
|
||||||
|
|
|
@ -51,7 +51,15 @@ module ActiveRecord
|
||||||
value = value.attributes[reflection.primary_key_column.name] unless value.nil?
|
value = value.attributes[reflection.primary_key_column.name] unless value.nil?
|
||||||
end
|
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 = klass.connection.type_cast(value, column)
|
||||||
value = value.to_s[0, column.limit] if value && column.limit && column.text?
|
value = value.to_s[0, column.limit] if value && column.limit && column.text?
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,14 @@ class UniquenessValidationTest < ActiveRecord::TestCase
|
||||||
assert t2.save, "Should now save t2 as unique"
|
assert t2.save, "Should now save t2 as unique"
|
||||||
end
|
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
|
def test_validates_uniqueness_with_nil_value
|
||||||
Topic.validates_uniqueness_of(:title)
|
Topic.validates_uniqueness_of(:title)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue