Scott Doane
2005-Jul-08 17:04 UTC
Best Practices: When to Put Definitions in Model or Controller
I''ve recently purchased Agile Web Development with Rails from the folks at Pragmatic Programming. It''s been invaluable, but some things still confuse me and I''d rather learn properly the first time. For instance my biggest confusion is when to put logic (definitions) in the Model, and when to put them in the Controller. I know you can do both, but for the life of me I can''t figure out when it''s best to put what where? Should more database level definitions go into the Model, and should more processing/manipulation definitions go into the Controller. Am I completely off thinking I should put anything in the Model aside from validations and relations? Part of my confusion lies in the fact I''m not a trained programmer, and even this whole MVC system is new to me. Any help would be appreciated. Thanks -Scotty
Sean T Allen
2005-Jul-08 17:22 UTC
Re: Best Practices: When to Put Definitions in Model or Controller
As a general rule of thumb with MVC... the model represents your data and basic manipulations on it... the view is well... the display the controller is a connection between the two... it takes requests from the view ( in this case over http ) and gets the data from the model and does any formatting transitions that the view might need... if something is intrinsic to the data, put it in the model... if it exists to interoperate with the view... then put it in the controller so something like lets say currency conversions on a bank account... i''d call that intrinsic to the data... transfering between accounts... again intrinsic... processing/manipulation... think of the model for what it is... in this example Account what can you do on an account... that would generally go in the model the controller is there to massage the input from the view into something the model can understand... ( for example finding the accounts to transfer between ) and returning the result to the view again as i said... general rule of thumb... there is no hard and fast... both of those however would come under Scott Doane wrote:> I''ve recently purchased Agile Web Development with Rails from the > folks at Pragmatic Programming. It''s been invaluable, but some things > still confuse me and I''d rather learn properly the first time. > > For instance my biggest confusion is when to put logic (definitions) > in the Model, and when to put them in the Controller. I know you can > do both, but for the life of me I can''t figure out when it''s best to > put what where? Should more database level definitions go into the > Model, and should more processing/manipulation definitions go into > the Controller. Am I completely off thinking I should put anything in > the Model aside from validations and relations? > > Part of my confusion lies in the fact I''m not a trained programmer, > and even this whole MVC system is new to me. Any help would be > appreciated. > > Thanks > > -Scotty > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Belorion
2005-Jul-08 17:23 UTC
Re: Best Practices: When to Put Definitions in Model or Controller
On 7/8/05, Scott Doane <psifire-ee4meeAH724@public.gmane.org> wrote:> I''ve recently purchased Agile Web Development with Rails from the > folks at Pragmatic Programming. It''s been invaluable, but some things > still confuse me and I''d rather learn properly the first time. > > For instance my biggest confusion is when to put logic (definitions) > in the Model, and when to put them in the Controller. I know you can > do both, but for the life of me I can''t figure out when it''s best to > put what where? Should more database level definitions go into the > Model, and should more processing/manipulation definitions go into > the Controller. Am I completely off thinking I should put anything in > the Model aside from validations and relations? > > Part of my confusion lies in the fact I''m not a trained programmer, > and even this whole MVC system is new to me. Any help would be > appreciated. > > Thanks > > -ScottySomeone correct me if I am off, but my personal defintions when using MVC are: Model: represents the data Controller: processes the data View: displays the data If I can''t decide whether to put a method in model or controller, I ask myself "Does this method represent the underlying data, or am I *using* the underlying data to perform some function or logic? If you answer yes to the first, then it belongs in the model, yes to the second probably in the controller. For example, I have a model, Author. My database has the fields ''last_name'' and ''first_name'', and in my model, I do: def name "#{first_name} #{lastname}" end name() is just another representation of the underlying data. Matt That being said, there will always be exceptions.
Raghu Havaldar
2005-Jul-08 17:31 UTC
Re: Best Practices: When to Put Definitions in Model or Controller
Scott, Model - where business rules/definitions go. The objects are called ''domain'' objects. Examples are User, Employee, Manager. Controller - manipulates the model. It contains ''user actions''. Any action on the system is directed at a controller, which then interacts and changes the Model. Finally, the View (directed to by the Controller) displays the Model (in a specific state) Here''s a good reference: http://c2.com/cgi/wiki?ModelViewController you can peruse Examples of MVC are abound. Some more popular ones are: Struts, WebWork -Raghu Havaldar On 7/8/05, Scott Doane <psifire-ee4meeAH724@public.gmane.org> wrote:> I''ve recently purchased Agile Web Development with Rails from the > folks at Pragmatic Programming. It''s been invaluable, but some things > still confuse me and I''d rather learn properly the first time. > > For instance my biggest confusion is when to put logic (definitions) > in the Model, and when to put them in the Controller. I know you can > do both, but for the life of me I can''t figure out when it''s best to > put what where? Should more database level definitions go into the > Model, and should more processing/manipulation definitions go into > the Controller. Am I completely off thinking I should put anything in > the Model aside from validations and relations? > > Part of my confusion lies in the fact I''m not a trained programmer, > and even this whole MVC system is new to me. Any help would be > appreciated. > > Thanks > > -Scotty > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >