Ruby on Rails
Friday, September 18, 2015
On Thu, Sep 17, 2015 at 2:44 PM, Hunter Stevens <onebree@gmail.com> wrote:
> The docs I was referring to were Rails Guides, which I take for
> granted as THE docs. Thank you for the appropriate link!
The Guides are great, but definitely not all the documentation! :) The
api documentation is probably more vital to understanding how Rails
actually works; after that it's straight into the code. :)
> 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 [whole] 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.
The `validates_numericality_of` method has a few options, as you've
seen. There are two that are important here, notably by *not* using
them in your validation:
* `allow_nil` -- if you set this explicitly to `false` or leave out as
the default is `false`, that will ensure that the value under test
*must* not be nil, i.e., must be present.
* `allow_blank` -- like `allow_nil` but also allows in the case of a
numerical test a "blank" value, i.e., an empty string. Again,
explicitly setting it to `false` or leaving it out as the default is
`false` will ensure that your value under test cannot be an empty
string.
I'm pretty sure that covers the gamut of your needs, so to make it
explicit, this is what you should have:
validates_numericality_of :number_of_extensions, { only_integer: true, greater_than: 0}
And that will ensure the number of extensions MUST be a positive,
whole number and can NOT be nil.
> Thank you for the help so far
My pleasure. :)
If you'll indulge me in a little more exposition, read Justin Weiss's
["Practicing Rails"](https://www.justinweiss.com/practicing-rails/),
most especially the first (free!) chapter, which
defines exquisitely a practice of learning something. Breaking things
down into a small, learnable chunk, by spooling up a small rails app
expressly for that chunk serves me so well in studying, playing around
with, explaining, and so on. Coupled with playing around in the rails
console, especially when using pry, helps so much. It becomes easier
to just try stuff out and see how it works, or doesn't!
> 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/CAHUC_t9WAJNFv7M%3D9yErRK%2B0w9SLBEf%2BXPs8K%3DzM5oKQJgeaFw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment