I am very new to ruby and rails, I wanted to have an attribute in a database which is a number
->The number can have a maximum length of 10.
->The number is separated into 3 parts, each part being delimited by a “-“.
->The first part from the left can have 2-7 digits, the second part should have exactly 2 digits
and the third part should have exactly 1 digit.
for example
23-56-7
3454-54-3
3435464-54-3 are valid numbers,
How do i do this?
Well it depends , it’s not a number if it contains ‘-‘ so you’ll probably want to store it as a string and validate it at creation .
There are 2 ways you can do that , from the database or from rails .
I’ll suggest validating it from rails model .
http://guides.rubyonrails.org/active_record_validations_callbacks.html
You’ll probably want to user a regex for the validations .
http://www.rubular.com
And if you want to perform calculations on the numbers you can easily do something like this
this will return an array with 3 integers .