My setup: Rails 2.3.10, Ruby 1.8.7
I need to update multiple instances of a model for a transaction. Should I make a class method and updates all the instances in the method or should I move that logic to the controller and updates each instance via an instance method for the model? I guess it is a tradeoff between fat controller vs. fat model and the general advice is fat model over fat controller.
Neither. If it’s a significant piece of logic, why not incorporate it into a dedicated class?
Alternatively, if your (I’m assuming) form data can be configured thus:
You can quite easily do a group update in your controller with:
More information about the details you’re updating and where they’re coming from could help.
EDIT: After reading your response below…
There’s a few ways you could do it. You could implement
Model.winandModel.loseas class methods to incorporate the logic, then simply call those methods from your controller:Or, even as a single method call:
Personally, if the only child objects involved are all instances of the same model, I’d implement this logic within the class itself.
However, if you’re bringing a variety of classes into the mix, it might be worth encapsulating it into a separate object altogether:
Either way, your actual transaction block should not be in the Controller.