Where to put this logic? Controller or Model?

3

My application has the following logic:

  • Given a received link, the application takes the <title> attribute of the page and transforms this attribute into the link description and saves it to the database.

I put this logic in callback before_save in Model and it works perfectly.

This type of logic, should I put in Controller or is the correct location even in the model?

    
asked by anonymous 31.01.2014 / 12:57

3 answers

3

You need to split this action into:

  • Find the link, make the transformation in the description (this is a function of the controller)
  • Save to database (this is model function)

  • As a rule the persistence of data, save in the database, is always function of the model. The logic of your application, depending on the type can go in the View (an initial filter in the data entry for example) and / or the Controller.

        
    31.01.2014 / 13:31
    10

    According to the MVC standard, all logic, especially database-related, should be placed in the Model.

    Consider the MVC standard as a computer. The Model is the CPU, the View is your monitor and the Controller are the ports / cables.

    Views have no logic, just like your monitor. It's dumb, it just shows what the controller (cables / ports) sends it.

    Controllers contain the least possible logic, as well as cables and ports: They are just what connects Views and Models.

    Models, like the CPU, do all the heavy lifting: it is responsible for any light or heavy logic (the most common being persistence). It is she who treats everything that the Controller sends her; and the Controller sends as received, who will receive, treat, format and manipulate will be the Model.

        
    31.01.2014 / 13:19
    8

    Your models (Models in Rails) are abstractions of the domain of your application. They should not have Crawling , Parsing logics, things like that.

    In my opinion, this logic of extracting information from a link should be performed by another object, a Service .

    Try to read Eric Evans's Domain Driven Design . They also have a nice infoQ summary .

    More on Wikipedia

        
    31.01.2014 / 13:16