I generated a Ruby 2.0 app and created a Portfolio model with two data
The default ACID functions work fine. But I want to add constraints
one the New and Edit functions for symbol:
- Can’t be blank
- Must be uppercase
- May not duplicate an existing symbol
ADWR shows me how to do this with Rails 1.x validation. Is there any
documentation that shows how this changed in Rails 2.0?
Thanks in Advance,
You can use validates_format_of and a regex to make sure it’s all
uppercase. But instead of raising an error if it’s not, why can’t you
just convert it to uppercase automatically? If you are using a
case-sensitive database, you can use the before_validation callback.
That way your validates_uniqueness_of will work properly. Otherwise, you
could do it in the before_save callback.
I am working on a project right now in which the client wants all user
entered data to be uppercase, so I wrote a simple method that I can call
from any controller
def upcase_data(data_hash, exclusions = )
exclusions += [:controller, :action, :return_to_controller,
data_hash.each_pair do |key, value|
data_hash[key] = value.upcase if value.class.name == ‘String’ &&
I never want to upcase controller or action, and then there are some
others that I don’t want to touch, which is why I have exclusions. I
call it like
upcase_data(params[:person], [:email, :sms_domain, :time_zone,
at the beginning of save methods. It works well for me.