validation - Rails where to place your Activemodel::validators -



validation - Rails where to place your Activemodel::validators -

this 2 part question.

part 1 if model writing validations inhering activerecord::base need include activemodel::validations within class?? api rails doesnt here in yehudakatz blog seems hint that?

part 2 best place set these validator files? under helpers or new model or in lib?

my current validator looks this

class gendervalidator < activemodel::validator def validate(record) cred = /(\d{6})(\d{4})(\d{1})(\d{2})/.match(record.id_number.to_s) #breaks id relevent sections namely birthdate, gender, nationality, validator. unless cred[0][/\d{13}/] #checks see if id valid length of numbers if isnt skip validation of gender homecoming true else birthdate = cred[1] #returns 6 digit string 'yymmdd' parsed_gender = cred[2] #returns 4 digit string '0001-4999:female 5000-9999:male' nationality = cred[3] # should homecoming either 1 or 0 1 if person foreign or 0 if person southafrican validate_gender(parsed_gender, record) end end private def validate_gender(parsed_gender, record) calculate_gender = (parsed_gender <= 4999 ? :female : :male) unless employee.gender == calculate_gender employee.errors[:gender] << "your id indicates have entered wrong gender" end end end

an valid id number of each person optional, if specify it should check see if gender correct.

if maintain in same model employees model error

actioncontroller::routingerror (uninitialized constant employee::gendervalidator): app/models/employee.rb:25:in `<class:employee>' app/models/employee.rb:1:in `<top (required)>' lib/role_requirement_system.rb:19:in `inherited' app/controllers/employees_controller.rb:1:in `<top (required)>' librato-rails (0.8.1) lib/librato/rack/middleware.rb:12:in `call'

so take cant in same file. best practice validations? watched rails casts , have read few blogs , quite new still.

edit

in model include class like

include activemodel::validations

and validations this

validates_presence_of :name, :position, :gender validate :instance_validations, :on => :create def instance_validations validates_with gendervalidator end

just incase wanted see ahead!

you not need include activemodel::validations

my preference to maintain validation objects in model folder.

so model gender have file gender.rb

for validator gendervalidator have file gender_validator.rb

so both files site in model folder.

here validator newsletter model

class newslettervalidator < activemodel::validator def validate(record) if record.send_test_email if test_email_address.blank? record.errors[:test_email_address] << "test email address blank" end if record.send_email_to_subscribers record.errors[:send_test_email] << "you cannot send test , send subscribers @ same time" end end end end

in newsletter model have

validates_with newslettervalidator

you have spelling error in example

you have

class gendervalidator < activemodel::validator

it should be

class gendervalidator < activemodel::validator

note capital v

ruby-on-rails validation activemodel

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -