Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveRecord - get all validations from a model

Is there a way to find all defined validations for a model. E.g :

class Temp < ActiveRecord::Base

     validate_uniqueness_of :name
     validate :some_method

     def some_method
        ...
     end
 end

When i try :

Temp.validators

It only finds the uniqueness validation, but not the other one.

like image 206
nitzan Avatar asked Oct 19 '22 15:10

nitzan


1 Answers

I've solved it by using :

Model._validate_callbacks.to_a.reject { |validation| validation.filter.to_s.starts_with?('validate_associated_records') }

The 'reject' is used to ignore some default validations.

like image 169
Haim Hamou Avatar answered Oct 23 '22 07:10

Haim Hamou