<p>I have an application, built using MVC, that produces a view which delivers summary information across a number of models. Further to that, some calculations are performed across the different sets of data.</p>

<p>There's no clear single model (that maps to a table at least) that seems to make sense as the starting point for this, so the various summaries are pulled from the contributing models in the controller, passed into the view and the calculations are performed there.</p>

<p>But that seems, well, <em>dirty</em>. But controllers are supposed to be lightweight, aren't they? And business logic shouldn't be in views, as I have it as present.</p>

<p>So where should this information be assembled? A new model, that doesn't map to a table? A library function/module? Or something else?</p>

<p>(Although I see this as mostly of an architectural/pattern question, I'm working in Rails, FWIW.)</p>

<p><strong>Edit</strong>: Good answers all round, and a lot of consensus, which is reassuring. I "accepted" the answer I did to keep the link to Railscasts at the top. I'm behind in my Railscast viewing - something I shall make strenuous attempts to rectify!</p>

[{'answer_id': 26846, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 0, 'selected': False, 'text': "<p>Controllers don't have to be <em>that</em> lightweight.</p>\n\n<p>However if you have some calculations that only rely on the model/s then you probably just need some sort of model wrapper for the models to perform the calculation. You can then place that into the API for the view so the view gets the end result.</p>\n"}, {'answer_id': 26848, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 1, 'selected': False, 'text': '<p>Why not create a model that doesn\'t inherit <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html" rel="nofollow noreferrer"><code>ActiveRecord::Base</code></a> and execute the logic there (think the Cart class in Agile...With Rails).</p>\n'}, {'answer_id': 26996, 'author': 'Tim Sullivan', 'author_id': 722, 'author_profile': 'https://Stackoverflow.com/users/722', 'pm_score': 3, 'selected': True, 'text': '<p>As Brian said, you can create another model that marshals out the work that needs doing. There is <a href="http://railscasts.com/episodes/121-non-active-record-model" rel="nofollow noreferrer">a great Railscast</a> on how to do this type of thing.</p>\n\n<p>HTH</p>\n'}, {'answer_id': 27200, 'author': 'salt.racer', 'author_id': 757, 'author_profile': 'https://Stackoverflow.com/users/757', 'pm_score': 0, 'selected': False, 'text': '<p>You don\'t want the logic to be in the view. However you are free to create a database view. Except, rather than create it on the database side, create it as a new model. This will enable you to perform your calculations and your actual logic there, in one place. The pain of trying to keep your views in sync vs. the one time "pain" of creating the new model... I vote for a new model.</p>\n'}, {'answer_id': 27227, 'author': 'Jim', 'author_id': 1208, 'author_profile': 'https://Stackoverflow.com/users/1208', 'pm_score': 1, 'selected': False, 'text': "<p>Controllers don't have to map to specific models or views. Your model doesn't have to map one-to-one to a database table. That's sort of the idea of the framework. Separation of concerns that can all be tested in isolation.</p>\n"}]
['https://Stackoverflow.com/questions/26834', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1060/']