Why should a controller action invoke one model method different from the original find or new?

I have almost all the “general” statements in the functions of my model. The problem is that I get the following error when I need to use more than one of these functions in my controller:

The controller action must call one model method, different from the initial find or new

and the IDE explains in more detail that:

This check warns if the controller action contains more than one call to the model method, after the initial .find or .new. It is recommended that you implement all the business logic inside the model class and use the only way to access it.

Does this mean that all logic should be put into more complex model functions? I thought that the controller’s job was to call the model functions and pass the results to the view.

If I return the model function code back to the controller, everything will work, but I will get code duplication in all the actions of my controller.

So what is the right approach here?

+4
source share
1 answer

A warning message does mean that the logic should be placed in one model function, but not necessarily more complex. To avoid duplication of the model and / or problems with the bold model, you may need to introduce additional classes that the model relies on.

Yes, the operation of the control is to call the functions of the model, but only in the form of a thin veneer in accordance with this guide to inspect one function of the model for the action of the controller except for the initial creation / search.

I'm not sure I understand your comment about code duplication in your controller if you move functions back, since you can always introduce general functions at the controller level. But then again, this is not the recommended “thin controller” and “reasonably thin model” approach with supporting classes as needed.

+2
source

Source: https://habr.com/ru/post/1493820/


All Articles