Wednesday, January 13, 2010

[Rails] Re: Multiple validations on a field... grouping them? only

Marnen Laibow-Koser wrote:
>
> But we shouldn't. If you have a good reason, I'd like to hear it.
>

It's a personal preference. Some people put custom validators in libs
and others do it in initializers.

I decided to use initializers more because it makes sense for validators
and also because I had originally learned to do so right here:

http://guides.rubyonrails.org/activerecord_validations_callbacks.html

According to rubyonrails.org right in their
activerecord_validations-callbacks they state:

============= QUOTE from rubyonrails.org ==================
You can even create your own validation helpers and reuse them in
several different models. Here is an example where we create a custom
validation helper to validate the format of fields that represent email
addresses:

ActiveRecord::Base.class_eval do def self.validates_as_radio(attr_name,
n, options={}) validates_inclusion_of attr_name, {:in =>
1..n}.merge(options) end end

Simply reopen ActiveRecord::Base and define a class method like that.
You'd typically put this code somewhere in config/initializers.

===========================================================

Some items don't need to go into initializers but again, it's more of an
organizational approach to rails and if you can show me 100% that both
rubyonrails.org and me are wrong, please do so.

Thanks mate. :)
--
Posted via http://www.ruby-forum.com/.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate