<p>In Ruby on Rails Development (or MVC in general), what quick rule should I follow as to where to put logic.</p>

<p>Please answer in the affirmative - With <em>Do put this here</em>, rather than <em>Don't put that there</em>.</p>

[{'answer_id': 60676, 'author': 'Paul Wicks', 'author_id': 85, 'author_profile': 'https://Stackoverflow.com/users/85', 'pm_score': 2, 'selected': False, 'text': '<p>Well, it sort of depends upon what the logic has to deal with... </p>\n\n<p>Often, it makes sense to push more things into your models, leaving controllers small. This ensures that this logic can easily be used from anywhere you need to access the data that your model represents. Views should contain almost no logic. So really, in general, you should strive to make it so that you Don\'t Repeat Yourself. </p>\n\n<p>Also, a quick bit of google reveals a few more concrete examples of what goes where.</p>\n\n<p>Model: validation requirements, data relationships, create methods, update methods, destroy methods, find methods (note that you should have not only the generic versions of these methods, but if there is something you are doing a lot, like finding people with red hair by last name, then you should extract that logic so that all you have to do is call the find_redH_by_name("smith") or something like that)</p>\n\n<p>View: This should be all about formatting of data, not the processing of data.</p>\n\n<p>Controller: This is where data processing goes. From the internet: "The controller’s purpose is to respond to the action requested by the user, take any parameters the user has set, process the data, interact with the model, and then pass the requested data, in final form, off to the view."</p>\n\n<p>Hope that helps.</p>\n'}, {'answer_id': 60689, 'author': 'Søren Spelling Lund', 'author_id': 6149, 'author_profile': 'https://Stackoverflow.com/users/6149', 'pm_score': 4, 'selected': False, 'text': "<p>The MVC pattern is really only concerned with UI and nothing else. You shouldn't put any complex business logic in the controller as it controls the view but not the logic. The Controller should concern itself with selecting the proper view and delegate more complex stuff to the domain model (Model) or the business layer.</p>\n\n<p>Domain Driven Design has a concept of Services which is a place you stick logic which needs to orchestrate a number of various types of objects which generally means logic which doesn't naturally belong on a Model class.</p>\n\n<p>I generally think of the Service layer as the API of my applications. My Services layers usually map pretty closely to the requirements of the application I'm creating thus the Service layer acts as a simplification of the more complex interactions found in the lower levels of my app, i.e. you could accomplish the same goal bypassing the Service layers but you'd have to pull a lot more levers to make it work.</p>\n\n<p>Note that I'm not talking about Rails here I'm talking about a general architectural style which addresses your particular problem.</p>\n"}, {'answer_id': 60727, 'author': 'John Topley', 'author_id': 1450, 'author_profile': 'https://Stackoverflow.com/users/1450', 'pm_score': 3, 'selected': False, 'text': '<p>The Rails way is to have <a href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model" rel="noreferrer">skinny controllers and fat models</a>.</p>\n'}, {'answer_id': 60806, 'author': 'pauliephonic', 'author_id': 5374, 'author_profile': 'https://Stackoverflow.com/users/5374', 'pm_score': 8, 'selected': True, 'text': '<p>MVC</p>\n\n<p><strong>Controller</strong>: Put code here that has to do with working out what a user wants, and deciding what to give them, working out whether they are logged in, whether they should see certain data, etc. In the end, the controller looks at requests and works out what data (Models) to show and what Views to render. If you are in doubt about whether code should go in the controller, then it probably shouldn\'t. Keep your controllers <a href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model" rel="noreferrer">skinny</a>.</p>\n\n<p><strong>View</strong>: The view should only contain the minimum code to display your data (Model), it shouldn\'t do lots of processing or calculating, it should be displaying data calculated (or summarized) by the Model, or generated from the Controller. If your View really needs to do processing that can\'t be done by the Model or Controller, put the code in a Helper. Lots of Ruby code in a View makes the pages markup hard to read.</p>\n\n<p><strong>Model</strong>: Your model should be where <strong>all</strong> your  code that relates to your data (the entities that make up your site e.g. Users, Post, Accounts, Friends etc.) lives.  If code needs to save, update or summarise data related to your entities, put it here. It will be re-usable across your Views and Controllers.</p>\n'}, {'answer_id': 60824, 'author': 'jcoby', 'author_id': 2884, 'author_profile': 'https://Stackoverflow.com/users/2884', 'pm_score': 5, 'selected': False, 'text': "<p>To add to pauliephonic's answer:</p>\n\n<p><strong>Helper</strong>: functions to make creating the view easier.  For example, if you're always iterating over a list of widgets to display their price, put it into a helper (along with a partial for the actual display).  Or if you have a piece of RJS that you don't want cluttering up the view, put it into a helper.</p>\n"}, {'answer_id': 62586, 'author': 'srboisvert', 'author_id': 6805, 'author_profile': 'https://Stackoverflow.com/users/6805', 'pm_score': 3, 'selected': False, 'text': "<p>Do put stuff related to authorization/access control in the controller.  </p>\n\n<p>Models are all about your data.  Validation, Relationships, CRUD, Business Logic </p>\n\n<p>Views are about showing your data.  Display and getting input only.</p>\n\n<p>Controllers are about controlling what data goes from your model to your view (and which view) and from your view to your model.  Controllers can also exist without models.</p>\n\n<p>I like to think of the controller as a security guard/receptionist who directs you the customer(request) to the appropriate counter where you ask a teller (view) a question.  The teller (view) then goes and gets the answer from a manager (model), who you never see.  You the request then go back to the security guard/receptionist (controller) and wait until you are directed to go another teller (view) who tells you the answer the manager (model) told them in response to the other teller's (view) question.  </p>\n\n<p>Likewise if you want to tell the teller (view) something then largely the same thing happens except the second teller will tell you whether the manager accepted your information.  It is also possible that the security guard/receptionist (controller) may have told you to take a hike since you were not authorized to tell the manager that information.</p>\n\n<p>So to extend the metaphor, in my stereotyped and unrealistic world, tellers (views) are pretty but empty-headed and often believe anything you tell them, security guard/receptionists are minimally polite but are not very knowledgeable but they know where people should and shouldn't go and managers are really ugly and mean but know everything and can tell what is true and what isn't.</p>\n"}, {'answer_id': 63619, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': '<p>One thing that helps separate properly is avoiding the "pass local variables from controller to view" anti-pattern.  Instead of this:</p>\n\n<pre><code># app/controllers/foos_controller.rb:\nclass FoosController &lt; ApplicationController\n\n  def show\n    @foo = Foo.find(...)\n  end\n\nend\n\n#app/views/foos/show.html.erb:\n...\n&lt;%= @foo.bar %&gt;\n...\n</code></pre>\n\n<p>Try moving it to a getter that is available as a helper method:</p>\n\n<pre><code># app/controllers/foos_controller.rb:\nclass FoosController &lt; ApplicationController\n\n  helper_method :foo\n\n  def show\n  end\n\n  protected\n\n  def foo\n    @foo ||= Foo.find(...)\n  end\n\nend\n\n#app/views/foos/show.html.erb:\n...\n&lt;%= foo.bar %&gt;\n...\n</code></pre>\n\n<p>This makes it easier to modify what gets put in "@foo" and how it is used.  It increases separation between controller and view without making them any more complicated.</p>\n'}, {'answer_id': 506268, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': "<p>Testing, Testing ...\nPut as much logic as possible in the model and then you will be able to test it properly. Unit tests test the data and the way it is formed by testing the model, and functional tests test the way it is routed or controlled by testing the controllers, so it follows that you can't test the integrity of the data unless it is in the model.</p>\n\n<p>j</p>\n"}, {'answer_id': 19000530, 'author': 'maddin2code', 'author_id': 1593422, 'author_profile': 'https://Stackoverflow.com/users/1593422', 'pm_score': 4, 'selected': False, 'text': '<p>Perfect explanations here already, one very simple sentence as conclusion and easy to remember:</p>\n\n<blockquote>\n  <p>We need SMART Models, THIN Controllers, and DUMB Views.</p>\n</blockquote>\n\n<p><a href="http://c2.com/cgi/wiki?ModelViewController">http://c2.com/cgi/wiki?ModelViewController</a></p>\n'}, {'answer_id': 39275860, 'author': 'Anutosh', 'author_id': 6761831, 'author_profile': 'https://Stackoverflow.com/users/6761831', 'pm_score': 0, 'selected': False, 'text': '<p>In simple terms, generally,\n<strong>Models</strong> will have all the codes related to table(s), their simple or complex relationships (think them as sql queries involving multiple tables), manipulation of the data/variables to arrive at a result using the business logic.</p>\n\n<p><strong>Controllers</strong> will have code/pointers towards the relevant models for the job requested.</p>\n\n<p><strong>Views</strong> will accept the user input/interaction and display the resultant response.</p>\n\n<p>Any major deviation from these will put unwanted strain on that part and the overall application performance may get impacted.</p>\n'}]
['https://Stackoverflow.com/questions/60658', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2167252/']