Deepak Dargade

I'm a Mumbai-based Entrepreneur, Ruby on Rails monomaniac and Food enthusiast.

29 Feb 2012

Yesterday i was working on destroy action of a model in. Before deletion I had to check whether the model has any child model or not. Even the model has child model, but deletion was successful even though i had set to false.

So i got curious and checked the rails issues. There i got to know a important concept of position of different members of model class in rails.

In my case i had placed belongs_to, has_many, validations and then callbacks.

So if I am using before_destroy, the rails follows the same sequence in my model. It first deletes the associated models if dependent destroy is mentioned, then checks for validations and then my before_destroy callback where i had explicitly told it to return false if child model size > 0. But since child model are already destroyed the size is 0 and my condition in before_destroy fails.

The solution is to place before_destroy above all.

So now rails will call callbacks first and then associations and validations.

It also helps in reducing the number of sql queries executed. In my case it reduced from 25 to 5. pretty good.



Looking for comments?

I don't have comments on this site as they're difficult to manage and take up too much time. I'd rather concentrate on producing content than managing comments.

Since there are no comments, feel free to contact me ✉️ contact me instead.