Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why controller action should call one model method other than an initial find or new?

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?

like image 668
gotqn Avatar asked Jul 27 '13 15:07

gotqn


People also ask

What decide which controller receives which request?

Routing decides which controller receives which requests.

Can a controller call another controller?

Yes, you can call a method of another controller. The controller is also a simple class. Only things are that its inheriting Controller Class. You can create an object of the controller, but it will not work for Routing if you want to redirect to another page.

What are the controller actions in Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.

Does every model need a controller?

Do I need a controller for each model? No, not necessarily. However, having one controller per RESTful resource is a convention for a reason, and you should carefully analyze why that convention isn't meeting your needs before doing something completely different.


1 Answers

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.

like image 176
Peter Alfvin Avatar answered Oct 11 '22 08:10

Peter Alfvin