Rails: Helpers and Models - where to organize code?

Your specific example is including a business rule in the sense that if the instance of the model is both purchased and confirmed then the proper status is "purchased" not "confirmed.

Your specific example is including a business rule in the sense that if the instance of the model is both purchased and confirmed then the proper status is "purchased" not "confirmed" So in your example, I'd definitely put the method in the model since it is coding one of your applications business rules. A different example: def status_string case status when 0: "Purchased" when 1: "Confirmed" else "Pending" end end In this case, the status_string method could be reasonably defined either in a View Helper or Model--it has nothing to do with any business rules, it is changing the representation of the value. I'd put it in the model since I tend to only put html-related sw into the View Helpers.

But depending on your internationalization scheme, a similar method might be better placed in the View Helper. A good example of a View Helper is an application-wide method to transform date time values into the standard representation for your app. Eg # application_helper.

Rb def date_long_s(d) d. Strftime("%A, %b *%d, %Y *%I:%M %p") end.

1 for - "I'd definitely put the method in the model since it is coding one of your applications business rules. " – Srikanth Venugopalan May 3 '10 at 4:06 another +1 for whatever Whiskey said. – Anurag Jun 18 '10 at 2:12.

This is really subjective and I agree, sometimes it is not clear if something belongs in a model or helper. For example: # using model status? Status.

Nice_name : "Pending" # using helper nice_name(status) The clear advantage here for the helper is that it can handle nil objects gracefully keeping views clean. The disadvantage is that the code is now in a different location away from the model Performance wise you will not see any significant difference between using helpers and models. It is more likely that DB round trips to pull status objects will be a bottleneck.

I use constant hashes in this kind of situations. Hash is defined in model file like this STATUS = { 1 => "Pending", 2 => "Confirmed" } I also declare constants for each status like this. ST_PENDING = 1 Declaring this is useful when writing queries.

For example, MyModel. All(:status=>ST_PENDING) status field in database table is number. So when printing, I simply use this.

MyModel::STATUSobj.status.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions