How to present cross model information in MVC?

I have an application built using MVC that creates a view that provides summary information for several models. In addition, some calculations are performed on different data sets.

There is no clear unified model (which, at least, compares with the table), which, apparently, makes sense as a starting point for this, therefore, various resumes are pulled from the providing models in the controller, transferred to the presentation and calculations are performed there.

But it seems, well, dirty. But controllers should be light, right? And business logic should not be in the minds, as I have it as present.

So where should this information be collected? A new model that does not map to a table? Library function / module? Or something else?

(Although I see this as a basic architecture / template issue, I work in Rails, FWIW.)

Change Good answers in everything and a lot of consensus, which is encouraging. I “accepted” the answer I made to keep the link to the Railscasts at the top. I lag behind in my viewing of Railscast - I will do something with explicit attempts to fix it!

+2
source share
5 answers

As Brian said, you can create another model that marshals the work that needs to be done. There is an excellent Railscast on how to do this.

NTN

+2
source

Why not create a model that does not inherit ActiveRecord::Base and execute the logic there (think the Cart class in Agile ... With Rails).

+1
source

Controllers do not need to map specific models or views. Your model should not match a database table. This is the idea of ​​structure. Separation of problems that can be tested in isolation.

+1
source

Controllers should not be so light.

However, if you have some calculations that depend only on the / s model, you probably just need some kind of model shell for the models to perform the calculation. You can then put this in the API for the view so that the view gets the final result.

0
source

You do not want the logic in the view. However, you can create a database view. In addition, instead of creating it on the database side, create it as a new model. This will allow you to perform your calculations and your actual logic there, in one place. The pain in trying to keep your views in sync compared to the one-time “pain” in creating a new model ... I will vote for the new model.

0
source

All Articles