Ruby on Rails Thursday, September 17, 2015

Thank you, I did not think of including `greater_than: 0` in the validations!

The docs I was referring to were Rails Guides, which I take for granted as THE docs. Thank you for the appropriate link!

Finally, just to clarify (because I want to make sure I understand):

1. The database allows NULL for :number_of_extensions
2. The sub-model requires :number_of_extensions to NOT be nil or blank
3. The sub model requires :number_of_extensions to be a while number >0

Given that information, if I only check validity for numericality, does Rails implicitly accept that as "must be present"? Because it is crucial that the database must allow null for all cases, but that Rails does not accept nil for this particular sub-model.

Thank you for the help so far

On Thursday, September 17, 2015 at 8:20:37 AM UTC-4, tamouse wrote:


On Wed, Sep 16, 2015 at 9:54 AM, Hunter Stevens <one...@gmail.com> wrote:
Hello. I know the title may be broad, but I am not sure how else to put it.

I am writing a model, in which the attribute "number_of_extensions" needs 2 things:
1. It is required for this model
2. It must be a whole, positive number

I know I can do this simply by

validates :number_of_extensions, presence: true, numericality: { only_integer: true }

However, if I validate for JUST numericality, is it assumed that the attribute SHOULD be present?



That is so; there is an option to allow nil or blank, but it is false by default. As it needs to be positive, you should also specify `greater_than: 0` in the options hash.

 
Also, while I am talking about validations, I have another question:

I know that the following type of validations is not in the docs anymore, but it is still supported. Would the following line be possible?

validates_numericality_of :number_of_extensions, :number_of_sidecars, :number_of_phones, only_integer: true




I'm pretty sure that is still in: 



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2a4a62eb-bca5-4883-bd28-fee33d810c6f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment