views:

30

answers:

2

How cat I get list of validations defined in model

Example:

class ModelName
  validates_presence_of :field_name
  validates_inclusion_of :sex, :in => %w(M F)
end

I need Hash like:

{:field_name => 'required', :sex => 'Must be in: M, F'}
+2  A: 

Looks like there's no native way to do it, but a quick Google (for "rails reflect validations") turns up this plugin.

Chris
Thank you, it seems that I need
manzhikov
+5  A: 

You don't need a plugin for basic needs.

You can do this to get a hash of all validators.

ModelName.validators

If you want to get the validators for a specific field :

ModelName.validators_on(:attribute)
slainer68
Cool! It's better, than plugin
manzhikov
Yup, agreed. Way better.
Chris
Just a note, this is only available in Rails 3. For earlier rails apps, the plugin is the way to go.
Jaime Bellmyer