How do I use rails validates for length to check for one value or another, not a range? -


i trying write model books in rails app , want validate isbn attribute there 2 possible lengths isbn: 10 , 13. how use validates make sure given isbn either 10 or 13 numbers long?

i thought using range:

validates :isbn, length: { minimum: 10, maximum: 13 } 

but if somehow 11 or 12 numbers { should_not be_valid }.

is there way this?

you can use custom validator purpose:

class book < activerecord::base   attr_accessible :isbn    validate :check_length    def check_length     unless isbn.size == 10 or isbn.size == 13       errors.add(:isbn, "length must 10 or 13")      end   end end 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -