Noob question for Ruby on Rails- here’s an example of my situation: If I have model circle and attribute radius, where do I do the calculations for circumference? Would this be in the model or the controller, and how might it look? circumference would need to be accessible in my views.
Also, would I be correct in thinking that I don’t need to make circumference an attribute that’s part of my model/database since it can be derived from a user-input radius?
The logic for calculating a derived attribute absolutely belongs in the model. The circumference is a property of the circle itself, not a concern of how you’re presenting it in the web interface.
In order to access the circumference from anywhere, just define a method on the class, such as the following:
Since it’s pretty cheap computationally to calculate the circumference, you can just calculate it as needed. If it were something that involved more complex calculation that you didn’t want to run multiple times, you could memoize it as follows:
That would set the
@circumferenceinstance variable the first time the method is called, then return the result of the first calculation on every subsequent call. If you were doing that, you’d need to make sure to set@circumferencetonilwhen the radius changed to make sure it’s accurate.