diff options
author | TheMonster <alwahsh.ahmed@gmail.com> | 2014-02-26 03:24:44 +0200 |
---|---|---|
committer | TheMonster <alwahsh.ahmed@gmail.com> | 2014-02-27 23:54:49 +0200 |
commit | 6e53d92498ada27cea6c7bae85708fcf5579223c (patch) | |
tree | d2f95b5c979111c68c738a102d0ff610a4f7ea32 /activerecord/lib/active_record/validations | |
parent | 14e697cf2a1f2a4d223463839f6748fca4658bcf (diff) | |
download | rails-6e53d92498ada27cea6c7bae85708fcf5579223c.tar.gz rails-6e53d92498ada27cea6c7bae85708fcf5579223c.tar.bz2 rails-6e53d92498ada27cea6c7bae85708fcf5579223c.zip |
Fix a bug affecting validations of enum attributes
This fixes a bug where any enum attribute of a model
would be evaluated always as 0 when calling the
database on validations.
This fix converts the value of the enum attribute
to its integer value rather than the string before
building the relation as the bug occured when the
string finally gets converted to integer using
string.to_i which converts it to 0.
[Vilius Luneckas, Ahmed AbouElhamayed]
Diffstat (limited to 'activerecord/lib/active_record/validations')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 7ebe9dfec0..49b0f73219 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -13,6 +13,7 @@ module ActiveRecord def validate_each(record, attribute, value) finder_class = find_finder_class_for(record) table = finder_class.arel_table + value = map_enum_attribute(finder_class,attribute,value) value = deserialize_attribute(record, attribute, value) relation = build_relation(finder_class, table, attribute, value) @@ -91,6 +92,12 @@ module ActiveRecord value = coder.dump value if value && coder value end + + def map_enum_attribute(klass,attribute,value) + mapping = klass.enum_mapping_for(attribute.to_s) + value = mapping[value] if value && mapping + value + end end module ClassMethods |