Forum: Ruby on Rails Understanding the rails control flow better ...

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.
04843ca9df3fdd590ae4f402d253bf41?d=identicon&s=25 Kedar Mhaswade (Guest)
on 2009-04-23 09:09
(Received via mailing list)
In order to understand exactly when the various model validators are
called, I installed ruby-debug and added “debugger” just before say
validates_numericality field of a model (say Product). I see that this
gets called (debugger break-point is reached) when I am just “Show”ing
the products (e.g. http://localhost:3000/products/4). It’s counter-
intuitive to me that just showing (not updating) a product should
result in a call to model validation.

Can someone please explain?

-Kedar
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-23 09:15
(Received via mailing list)
On Apr 23, 4:02 am, Kedar Mhaswade <madhuprab...@gmail.com> wrote:
> In order to understand exactly when the various model validators are
> called, I installed ruby-debug and added “debugger” just before say
> validates_numericality field of a model (say Product). I see that this
> gets called (debugger break-point is reached) when I am just “Show”ing
> the products (e.g.http://localhost:3000/products/4). It’s counter-
> intuitive to me that just showing (not updating) a product should
> result in a call to model validation.
>
> Can someone please explain?

It would help if you could show your model, and perhaps look at the
stack trace when you hit your breakpoint. To me it sounds like you've
got

class Product
  ...
  debugger
  validates_numericality_of ...
end

That breakpoint won't get hit when validations run, it will get hit
when the class is loaded.

Fred
D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2009-04-23 12:17
(Received via mailing list)
The validation is not "called".

a line like the following:

validates_numericality_of :whatever

is positioned directly in the class, not inside a class method.
therefore, this method is run when the class is loaded by the
application (which is on application startup,). Normal ruby beahviour
for methods called directly in a classes body.
As in development mode, classes are reloaded again on each request
(well, actually lazy-loaded since 2.3, so only loaded when the app
actually uses them), this code gets run everytime the Model is used in
any way.

However, the above line doesn't actually run the validation, it
CREATES/ASSIGNS the validation, which in turn isn't  run until the
instance gets validated.

so your debugger breakpoint is simply in the wrong place.
This topic is locked and can not be replied to.