Forum: Ruby on Rails Why is this method protected? (from agile book)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Al C. (Guest)
on 2007-03-19 00:53
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.
Conrad T. (Guest)
on 2007-03-19 05:08
(Received via mailing list)
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
AndyV (Guest)
on 2007-03-19 14:38
(Received via mailing list)
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>
Al C. (Guest)
on 2007-03-24 01:29
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>

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?
Dave T. (Guest)
on 2007-03-24 16:02
(Received via mailing list)
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.
This topic is locked and can not be replied to.