Test if string can be downcased?

I have a Feature model, where I am adding a before_save:

before_save {|feature|
    downcase_value(feature)
}

def downcase_value(record)
    record.value.downcase!
end

This works fine, unless I have saved a date in the column, in which case i get an error:

undefined method `downcase!' for Tue, 06 Feb 1996:Date

So my question is this - how can I test if the value can be downcased most efficiently?


Solution 1:

You can replace downcase_value method body with:

record.value.downcase! if record.value.respond_to? :downcase!

but if you want to save any value, just modify your code this way:

record.value.to_s.downcase!