In Ruby on Rails is the business rule on the model?

Asked

Viewed 716 times

8

In Java the business rule is in controller, but I heard a speaker saying that in Ruby on Rails the business rule is on model.

Someone confirms that to me?

  • 1

    I was going to formulate an answer, based on the belief that Model has nothing to do with Orms, this occurred with time-vs-need. In fact the Model are the "business rules", they access data and update, the only thing that it should not do to obtain data directly or send data to the output (display on screen), this clear targeting the organization.

1 answer

9


I decided to answer because there is no consensus on this. Which indicates that there are problems in the MVC standard. When something is really good, everyone agrees.

  • Treating business rules on the model is bad practice?

    Here the answers indicate that it is in model, save some very strong reason. All users who responded have good reputation here on the site.

  • How to implement a "slim controller"

    The answer with several votes indicates that the controller is not the right place.

  • Business Rules in the Database - what are the advantages and disadvantages?

    This discusses whether the business rule should be in the database or not. Now, if there are a lot of people who think they should be in the database, then if they’re in the application, the closest to the database is the model and not the controller.

  • What is Application Rules and what are the differences from Business Rules?

    This shows the difference between business and application rule. Perhaps the confusion is because some people consider as a business rule what is actually an application rule. The latter is in the controller. But we’re talking business rule.

  • In an MVC project, where I must leave the enums?

    One more showing you have to know what you’re doing everything for.

  • Which application layer is aimed at processing broader and more complex business rules?

    Here, in a reply, the accepted and most voted, talks to create an extra layer. That’s not quite MVC, right? Anyway it’s not to use in controller. There’s another one that goes the same way, although it’s a little confusing to say that.

  • Which Pattern is used to validate business rule?

    We have an answer that clearly states that you should stay in model and another that goes to other layers that are not part of the MVC, but was the person who said in the first question that was to put in the model, then not even the same person agrees with herself (she may be saying it depends on the scenario).

  • Where the MVC standard business rule should be?

    Now it’s complicated because the same person who responded there in the previous one is now saying it’s to put in the controller. There is another well-voted answer there that speaks that depends. From what I understand it goes in the way that people confuse business rule with application rule, but it talks in models, so it seems to tend to the same one we saw before. And there’s a well-voted third party that shows it depends on the case. It confuses business rule with enforcement rule. But it was answered by the same person who answered how to differentiate one from the other. If it is business rule you can trust that the view do this? You may not even have control of what is the view in the application.

  • It’s wrong to leave business rules on controllers?

    In that there is one depends again. My understanding that there is written that it should put in the controller unless it gets too big (aãã?!?!). The other answer goes the same way indicating that if the system is small can put in no controller. I find it very strange the decision depends on the size of the system. What I even think is that if the system is small should not use MVC. There are those who do not use MVC even in large systems, and get along very well with it. But changing the pattern according to the size of the system is weird.

In Java the business rule is in the controller

Is that right? Do you have any reference to it? All of Java? It seems to me to be a statement that is not supported in the form presented. But I believe that somewhere you should say that it is so. Is the source reliable? Are there other reliable ones that confirm this? Others that contradict? Are reliable even?

Completion

You may be thinking that Ror is different. Yes, it could be, but does MVC change according to language? It can’t. That’s what I’m talking about, we went our separate ways.

For me the controller is just a simple layer to make the model communicate with the vision, any extra operation that is used would be wrong.

I see that many people end up having difficulty getting the model because the model is not always the way they need it, so the controller has to get around it. And the problem is that the model to be used should be suitable for this whole operation and actually the model they use is something basic.

On the other hand it starts to get complicated. It starts to turn DDD over MVC. So people prefer to violate the standard to simplify the code, which I think is good, until it’s a problem. I think MVC was born for a simple context and today people need something more complex or something so simple that even MVC doesn’t make sense. But that part is still opinionated.

It’s good to make it clear that I have a tendency to dislike MVC, so despite what a lot of people say, maybe put business rules on controller it’s not so absurd. If he was that thin it might be the case to neither have it, and not adopt MVC. It took me a while to think about it, but it seems to me that the MVC exists just when the controller is complex. Without it, it has only a model and a vision, which a very simple logic already solves. Of course, it depends somewhat on the complexity of the routing as well. Putting direct access logic into the view makes each action be linked to a page directly, which is just what most systems need.

I’m not saying that business rules should be on the control layer, but MVC should only exist if this layer really has a lot of logic.

If you don’t have all this very clear, not knowing exactly what you’re doing in the specific case, you’ll probably make a bad decision.

So the simple answer is that you must do what seems right to you. But everything indicates that almost everyone agrees that the business rule goes on model and that to a lesser extent there is some agreement that the application rule should go on controller or view, according to need and complexity. What defines whether it is business rule or application is something a little open.

  • 1

    Very good response, 11 years of Rails and really the consensus is business rules in the model, with some remarks for use of Decorator/Services(or other pore as extra layer) for extraction of business rules not related to BD

  • I started in structured PHP, in college I studied java and now I chose to work with Ruby on Rails, so I was a little confused. I am new to ruby on Rails and have to improve a lot, but I hope to be as good as you seem to be Luiz Carvalho, thanks for the strength you have given me =D Thanks for the reply Bigown

Browser other questions tagged

You are not signed in. Login or sign up in order to post.