Thursday, April 3, 2008

Thick Models Thin Controllers

i have worked for about six months with Rails and through this time i was coding a lot in my controller which has shown to me lots of problem later on

for example,

i was having a Topic model and Post model, where every topic has many posts
and in each of these two models there is a field called archive

i was having a controller that changes the archive field to a certain value then pass on every post it owns and change its attribute also

later on, i made another controller and in it i also changed that field in it and passed on all posts to do the same change in it

this led to redundancy and duplication of code

but the problem became more complicated when i wanted to change the logic of this topic and its posts replication to a new way

here i have to move on my controllers and change this code in them

but i have added the code that observe this change in topic attribute and apply it on all its posts inside the model, i would haven't faced this problem anymore
and this will give me flexibility to do changes easily also

so this led to the idea of having my controller thin containing the least amount of code and my model thick containing large amount of code

No comments: