Understanding the rails control flow better ...

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

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

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.