Line drawing between model and controller

I am creating this calming application using RoR, and it's hard for me to draw a line between the things that should go along the model and the things that should go on the controller.

As an example, I have 7 methods on my controller (those that make it calm, i.e. index (), show (), create (), update () ...) and often find that additional ones need to be added methods, and do it by creating them as members.

What I would like to do here is collecting experience from you guys about what is happening there (where should I stick to all interactions with the databases on the model and just call these methods from the controller?)

In addition, adding things that are not related to the database to my controller, that is, I want to make an HTTP call to screen some data from the website.

HTTP calls can become large and messy. Should all this go to my controller, or will it be a separate class or module, and only be included in my controller so that it can be called?

If so, what would be the best way to do this?

I'm a little confused by all of this, so it would be great if someone introduced.

Thanks in advance

+7
ruby ruby-on-rails model-view-controller
source share
6 answers

This is part of Domain Driven Design .

A domain is a field of knowledge that defines the area with which the application is trying to work and solve problems.

The model layer is considered the domain level. This is where all the rules that define the domain or business logic are saved. The model acts as a filter between the real data and the rest of the application in a way that defines the domain.

The details of the Domain implementation (mySQL or MSSql or Webservice or Xml file or an external web page or something else) are hidden by the model.

The controller is only a messenger. Its task is to receive messages from the user and transfer them to the Model. The model comes up with the answer, and the controller performs the best way to pass this on to the user.

The view is similar to makeup, just make sure the data looks good and presentable to the user.

The website you are cleaning up can be considered part of the domain. This site contains knowledge that defines the world that your application defines. The scripting process glues this knowledge together in such a way that it will relate to the rest of the world that your application defines. The controller does not care about where this knowledge came from, it just passes the message to the view. View will fuss over the data and make it beautiful and send it to a user who completely does not pay attention to the whole process and just sees a pretty web page!

+3
source share

Generally speaking, a good approach (no religion flame, plz) is to have thin controllers and fat models. That way, you can also easily check the β€œthings” that models should do ...

+3
source share

Scraping material should go in non-ActiveRecord models, and the controller should just call the methods of this model.

There is a good example of a model without table AR and one non-AR model on railscasts .

So your controller will do something like:

def index @some_things = SomeThing.all end def show @some_thing = SomeThing.find params[......] end 

And your SomeThing model will implement the necessary logic.

+1
source share

I adhere to the rules of thin controllers and fat models.

Given a calm approach, I keep all the controllers in this standard and do not have additional methods that are outside the default value. If I need new functionality and note that the methods perform similar actions (or will have similar names, for example group_add, group_remove), I create a new controller and use existing models and create new methods in the model.

It also allows you to maintain a calm approach to these further actions in a meaningful way, and, in particular, each action has a limited specification for its work, it will never do more than one. Nothing worse than using an API call that performs two functions, depending on what it's called.

The approach I use is to process the request as fast as possible, using as little code as possible and abstracting the complex tasks / operations with the model.

+1
source share

When I used SOAP calls from Rails, which is similar to what you are talking about, I considered them as a data source, so, in my opinion, they belonged to the model. You can also choose if the functionality is somewhat generic, write a plug-in to perform this task, or find a plug-in that does most of what you want and use it.

If in doubt, think of your model as an application and your presentation / controller as an interface. If its incomprehensible interface probably belongs to the model.

This question may help.

0
source share

I also prefer skinny controllers and bold models ... A gem of Jose Valim's inherited_resources is a good tool to help you enforce this rule. I suggest you take a look at this: http://github.com/josevalim/inherited_resources

0
source share

All Articles