# Understanding the rails control flow better

**URL:** <https://www.ruby-forum.com/t/understanding-the-rails-control-flow-better/166262>\
**Category:** Rails\
**Created:** [April 23, 2009, 9:09am UTC](https://www.ruby-forum.com/t/understanding-the-rails-control-flow-better/166262 "2009-04-23T09:09:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [April 23, 2009, 9:09am UTC](https://www.ruby-forum.com/t/understanding-the-rails-control-flow-better/166262/1 "2009-04-23T09:09:19Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [April 23, 2009, 9:15am UTC](https://www.ruby-forum.com/t/understanding-the-rails-control-flow-better/166262/2 "2009-04-23T09:15:11Z")

</div>

On Apr 23, 4:02 am, Kedar M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [April 23, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/understanding-the-rails-control-flow-better/166262/3 "2009-04-23T12:17:06Z")

</div>

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.
