Rails: Helpers and Models - where to organize code

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2010-05-03T01:41:00Z Indexed on 2010/05/03 1:48 UTC
Read the original article Hit count: 314

Filed under:
|
|
|

More and more I'm putting all of my code in models and helpers concerning MVC.

However, sometimes I'm not sure where to organize code. Should it go into the model or should it go into a helper. What are the benefits of each. Is one faster or are they the same. I've heard something about all models getting cached so it seems then like that would be a better place to put most of my code.

For example here is a scenario that works in a model or in helper:

 def status
  if self.purchased
   "Purcahsed"
  elsif self.confirmed
   "Confirmed"
  elsif self.reserved
   "Reserved"
  else
   "Pending"
  end

end

I don't need to save this status as in the database because there are boolean fields for purchased, and confirmed, and reserved. So why put this in a model or why put it into a helper?

So I'm not sure of the best practice or benefits gained on putting code into a model or into helper if it can be in both.

© Stack Overflow or respective owner

Related posts about mvc

Related posts about ruby-on-rails