<p>Recently thanks to rails' popularity, many people start using activerecord as model. however, before I heard of rails (my peer group was not a fan of open source stuff, we were taught in a .NET school...) and while I was doing my final year project, i found this definition for a model</p>

<blockquote>
  <p>The model represents enterprise data and the business rules that govern access to and updates of this data. Often the model serves as a software approximation to a real-world process, so simple real-world modeling techniques apply when defining the model.</p>
</blockquote>

<p>it doesn't say the model should represent one table as what activerecord does. And normally within a transaction, one may have to query a few unrelated tables and then manipulate data from different tables... so if activerecord is used as model, then either one would have to cram all the logic code into the controller (which is kinda popular in some php frameworks) that makes it difficult to test or hack the activerecord model so that it performs database operation on not only the table it maps to, but also other related tables as well...</p>

<p>so, what is so great about abusing (IMHO) activerecord as the model in a MVC architectural pattern?</p>

[{'answer_id': 56578, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 4, 'selected': True, 'text': "<p>Martin Fowler described this pattern in Patterns of Enterprise Application Architecture together with two other patterns or architectures. These patterns are good for different situations and different amounts of complexity.</p>\n\n<p>If you want to so only simple stuff you can use Transaction Script. This is an architecture you saw in lot's of old ASP and PHP pages where a single script contained the business logic, data-access logic and presentation logic. This falls apart fast when things get more complicated.</p>\n\n<p>The next thing you can do is add some separation between presentation and model. This is activerecord. The model is still tied to the database but you've a bit more flexibility because you can reuse your model/dataccess between views/pages/whatever. It's not as flexible as it could be but depending on your data-access solution it can be flexible enough. Frameworks like CSLA in .Net have a lot of aspects from this patterm (I think Entity Framework looks a bit too much like this too). It can still handle a lot of complexity without becoming unmaintainable.</p>\n\n<p>The next step is separating your data-access layer and your model. This usually requires a good OR mapper or a lot of work. So not everyone wants to go this way. Lot's of methodologies like domain driven design perscribe this approach.</p>\n\n<p>So it's all a matter of context. What do you need and what is the best solution. I even still use transaction-script sometimes for simple single use code.</p>\n"}, {'answer_id': 56632, 'author': 'ottobar', 'author_id': 5830, 'author_profile': 'https://Stackoverflow.com/users/5830', 'pm_score': 1, 'selected': False, 'text': '<p>The great thing about using the Rails ActiveRecord as a model in MVC is that it gives you an automatic ORM (Object Relational Mapper) and easy way to create associations between models. As you have pointed out, MVC can sometimes be lacking.</p>\n\n<p>Therefore, for some complex transaction involving many models, I\'d suggest to use a Presenter in between your controller and your models (<a href="http://blog.jayfields.com/2007/03/rails-presenter-pattern.html" rel="nofollow noreferrer">Rails Presenter Pattern</a>). The Presenter would aggregate your models and transactional logic and would remain easily testable. You definitely want to strive to keep all of your business logic in your models or presenters, and out of your controllers (<a href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model" rel="nofollow noreferrer">Skinny Controller, Fat Model</a>).</p>\n'}, {'answer_id': 4263098, 'author': 'David Conde', 'author_id': 420447, 'author_profile': 'https://Stackoverflow.com/users/420447', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve said many times that using Active Record (or ORM which is almost the same) as Business Models is not a good idea. Let me explain:</p>\n\n<p>The fact that PHP is Open Source, Free (and all that long story...) provides it with a vast community of developers pouring code into forums, sites like GitHub, Google code and so on. You might see this as a good thing, but sometimes it tends not to be "so good". For instance, suppose you are facing a project and you wish to use a ORM framework for facing your problem written in PHP, well... you\'ll have a lot of <a href="http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software" rel="nofollow">options to choose for</a>:</p>\n\n<ul>\n<li>Doctrine</li>\n<li>Propel</li>\n<li>QCodo</li>\n<li>Torpor</li>\n<li>RedBean</li>\n</ul>\n\n<p>And the list goes on and on. New projects are created regularly. So imagine that you\'ve built a full blown framework and even a source code generator based on that framework. But you didn\'t placed business classes because, after all, "why writing the same classes again?". Time goes by and a new ORM framework is released and you want to switch to the new ORM, but you\'ll have to modify almost every client application using direct reference to your data model.</p>\n\n<p>Bottom line, Active Record and ORM are meant to be in the Data Layer of your application, if you mix them with your Presentation Layer, you can experience problems like this example I\'ve just laid.</p>\n\n<p>Hear @Mendelt\'s wise words: Read Martin Fowler. He\'s put many books and articles on OO design and published some good material on the subject. Also, you might want to look into <a href="http://www.antipatterns.com/" rel="nofollow">Anti-Patterns</a>, more specifically into <a href="http://www.antipatterns.com/vendorlockin.htm" rel="nofollow">Vendor Lock In</a>, which is what happens when we make our application dependent on 3rd party tools. Finally, I wrote <a href="http://davidcondemarin.blogspot.com/2010/11/using-orm-classes-as-business-models.html" rel="nofollow">this</a> blog post speaking about the same issue, so if you want to, check it out.</p>\n\n<p>Hope my answer has been of any use.</p>\n'}]
['https://Stackoverflow.com/questions/56546', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5742/']