# Why is this method protected? (from agile book)

**URL:** <https://www.ruby-forum.com/t/why-is-this-method-protected-from-agile-book/89234>\
**Category:** Rails\
**Created:** [March 18, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/why-is-this-method-protected-from-agile-book/89234 "2007-03-18T23:53:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [March 18, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/why-is-this-method-protected-from-agile-book/89234/1 "2007-03-18T23:53:13Z")

</div>

Hello,

This is form the 2.ed agile book in the product model:

/models/product.rb:

validates\_presence\_of :title, :description, :image\_url  
validates\_numericality\_of :price  
validates\_uniqueness\_of :title

protected  
def validate  
errors.add(:price, “should be at least 0.01”) if price.nil? || price \<  
0.01  
end

Why does the method that checks the price have to be protected?

Couldnt I, instead of the method just write:

validates\_numericality\_of :price, :minimum =\> 0.01

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [March 19, 2007, 4:08am UTC](https://www.ruby-forum.com/t/why-is-this-method-protected-from-agile-book/89234/2 "2007-03-19T04:08:49Z")

</div>

Hi, this would work because for the simply fact that :minimum isn’t a  
configuration option for validates\_numericality\_of. At this time, the  
config options are as follows:

Configuration options:

message - A custom error message (default is: “is not a number”)

on - Specifies when this validation is active (default is :save, other  
options :create, :update)

only\_integer - Specifies whether the value has to be an integer, e.g.  
an integral value (default is false)

allow\_nil - Skip validation if attribute is nil (default is false).  
Notice that for fixnum and float columsn empty strings are converted  
to nil

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [March 19, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/why-is-this-method-protected-from-agile-book/89234/3 "2007-03-19T13:38:34Z")

</div>

I think the response is twofold. As for AWDRv2, I think the point of  
the chapter is to demonstrate the different ways that validation can  
be added to a model. The first dozen or so chapters are dedicated to  
a somewhat real world example. Take them for what they are, realizing  
that you might want to do things differently.

As for your specific question, “validate” is a protected method of  
ActiveRecord::Base. If you want to extend it (the point of the AWDR  
example) then you’ve got to implement the full signature of the  
method, including it’s ‘protectedness’.

HTH,  
AndyV

On Mar 18, 6:53 pm, Al Cholic [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [March 24, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/why-is-this-method-protected-from-agile-book/89234/4 "2007-03-24T15:02:23Z")

</div>

On Mar 23, 2007, at 6:29 PM, Al Cholic wrote:

> Yes, I understand that the method is prected and the variables  
> inside it  
> can only be used inside the method itself. But, why does it need  
> to be  
> protected?

Because it never should be called from outside the context of the model.

Regards

Dave T.

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [March 24, 2007, 12:29am UTC](https://www.ruby-forum.com/t/why-is-this-method-protected-from-agile-book/89234/5 "2007-03-24T00:29:11Z")

</div>

AndyV wrote:

> I think the response is twofold. As for AWDRv2, I think the point of  
> the chapter is to demonstrate the different ways that validation can  
> be added to a model. The first dozen or so chapters are dedicated to  
> a somewhat real world example. Take them for what they are, realizing  
> that you might want to do things differently.
> 
> As for your specific question, “validate” is a protected method of  
> ActiveRecord::Base. If you want to extend it (the point of the AWDR  
> example) then you’ve got to implement the full signature of the  
> method, including it’s ‘protectedness’.
> 
> HTH,  
> AndyV
> 
> On Mar 18, 6:53 pm, Al Cholic [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Yes, I understand that the method is prected and the variables inside it  
can only be used inside the method itself. But, why does it need to be  
protected?
