diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-11-26 14:52:48 -0800 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-11-26 20:54:19 -0200 |
commit | 5fdbec7dd15a8e6edef861899accb8b3b69cc87a (patch) | |
tree | 773aef4aad2122afa76e74409d1d1edd21bd1c5f /activerecord/lib/active_record | |
parent | f7e4e37ae756e59da2e10bdeafd9458ef7189582 (diff) | |
download | rails-5fdbec7dd15a8e6edef861899accb8b3b69cc87a.tar.gz rails-5fdbec7dd15a8e6edef861899accb8b3b69cc87a.tar.bz2 rails-5fdbec7dd15a8e6edef861899accb8b3b69cc87a.zip |
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
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 38f37f5c8a..e2b132ca81 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -51,7 +51,15 @@ module ActiveRecord 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? |