7

I have almost all of the "shared" statements in functions in my model. The problem is that I am getting the following error, when I need to use more then one of these functions in my controller:

Controller action should call one model method other than an initial find or new

and the IDE goes deeper explaining that:

This inspection warns if a controller action contains more than one model method call, after the initial .find or .new. It’s recommended that you implement all business logic inside the model class, and use a single method to access it.

Is this mean that all of the logic should be put in more complex model functions? I have thought that the work of the controller is to call model functions and passes the results to the view.

If I put back the model functions code back to the controller, everything will work, but I will get a code duplication in all my controller actions.

So, what is the right approach here?

gotqn
  • 42,737
  • 46
  • 157
  • 243

1 Answers1

5

The warning message indeed means that the logic should be put in a single model function, but not necessarily more complex ones. To avoid model duplication and/or the "fat model" problem, you may need to introduce additional classes that the model relies on.

Yes, the work of the control is to call model functions, but only as a thin veneer, per this inspection guideline of one model function per controller action aside from an initial create/find.

I'm not sure I understand your comment about getting code duplication in your controller if you move functions back up, since you can always introduce shared functions at the controller level. But again, that's not the recommended approach of "thin controller" and "reasonably thin model" with supporting classes as required.

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • So, is there a rule to define when to create a new model method or shared method at the controller level? I am new in rails and actually, the only methods that I have in my controllers are the default ones (new/edit/update/etc) – gotqn Jul 27 '13 at 16:57
  • There is no hard and fast rule that I'm aware of, only a rule of "thin controllers" and "not fat models". And again, another choice besides the two you mention is to create a new class for use by the model such that you don't need additional methods in either the controller or the model. – Peter Alfvin Jul 28 '13 at 02:06
  • I am late to this thread. This has confused me a tad, because don’t we increase coupling this way in our models? Shouldn’t our classes me as Independent as possible so that we could reuse them in different parts of the application or designed on a way where they can be plugged into another application? How is this possible if we have thin controllers and the model methods are all calling methods from other classes? Is it not better for the controller to be bigger and control how we call and interact with the Model’s methods? – Kevin Greetham Nov 08 '22 at 08:53
  • @PeterAlfvin Can you assist. Is this a specific implementation choice of rails. I regularly have big controllers so that my methods are easy to test and my classes are more reusable and if I want to change anything I just change things inside the controller aswell as the method itself!! – Kevin Greetham Nov 08 '22 at 11:35