Reputation: 413
I have many methods in my models like this :
def calc
self.c = self.child.a * self.child.b
end
I can easy use @object.calc to get the result et use it in others calculations.
My question is what is the best way to do that. Keep this or store it in my database and upload it for each child update. If the calculation use more complex functions (squareroot etc...) or if I have calculation of calculation, may be it would be faster to store it, or calculate it in my database request.
Upvotes: 0
Views: 34
Reputation:
Depends on what you need it for. If it's simple usage than calculating it on the fly might be an acceptable expense, to buy lower programmer time needed (a real benefit!).
If you want to do any sort of filtering or ordering, then you'll probably want to store it in a db column and use some sort of before_save
hook to update the value.
If it's not a very expensive operation, like matrix multiplication, you might never notice the performance difference whichever way you go.
Upvotes: 1