The problem I have with Active Record is that it creates confusion about these two very different styles of programming. A database table is a data structure. It has exposed data and no behavior. But an Active Record appears to be an object. It has “hidden” data, and exposed behavior. I put the word “hidden” in quotes because the data is, in fact, not hidden. Almost all ActiveRecord derivatives export the database columns through accessors and mutators. Indeed, the Active Record is meant to be used like a data structure.
On the other hand, many people put business rule methods in their Active Record classes; which makes them appear to be objects. This leads to a dilemma. On which side of the line does the Active Record really fall? Is it an object? Or is it a data structure?
A Wikipedia resume as críticas em uma preocupação de testabilidade :
In OOP the concept of encapsulation is often at odds with the concept of separation of concerns. Generally speaking, patterns that favor separation of concerns are more suitable to isolated unit tests while patterns that favor encapsulation have easier to use APIs. Active Record heavily favors encapsulation to the point where testing without a database is quite difficult.
Especificamente para a implementação do Ruby on Rails, Gavin King escreve (grifo meu):
At this point, most developers are thinking um, ok, so how the hell am I supposed to know what attributes a Company has by looking at my code? And how can my IDE auto-complete them? Of course, the Rails folks have a quick answer to this question Oh, just fire up your database client and look in the database!. Then, assuming that you know ActiveRecord's automagic capitalization and pluralization rules /perfectly/, you will be able to guess the names of the attributes of your own Company class, and type them in manually.
Também na implementação do Ruby on Rails, John Januszczak escreve (ênfase minha):
PROBLEM #1: STATIC METHODS
Some would say using Static methods simply amounts to procedural programming, and therefore is poor Object Oriented design. Others would say static methods are death to testability.
PROBLEM #2: GLOBAL CONFIGURATION SETTINGS
Therefore there is no dependency injection on the Account class in my example, and by extension, on the Account instances. As we should all know by now, looking for things is very, very bad!
Mais alguns recursos sobre porque o ActiveRecord e o ORM são geralmente considerados anti-padrão:
- Desacoplar o modelo de domínio Rails do Activerecord no StackOverflow,
- O ORM é um antipadrão
- Associações sujas com o ActiveRecord
- Número 23: Princípios do projeto SOLID sobre como praticar o Ruby
O ActiveRecord sempre pareceu um anti-padrão extremamente útil , mas eu concordo que ele vai contra o SRP e, além disso, vai contra o princípio de inversão de dependência.