|
|
|
|
|
by e12e
2107 days ago
|
|
> Use activerecord uniquely as a querying mechanism (read or write), don't use relationships and don't put validations in there. I'm sorry, but without relationships - why even use AR for querying? And how? Via connection.select/execute? |
|
The problem is if you try to access relationships that you didn't load from the object, that shouldn't be done. It's the autoloading from the database that is the real problem.
If you have enough discipline and enough authority to ensure that never happens within the software you are working on, you can.
It's really culture problem. As soon as you pass around a `user`, someone will type `.posts` on it, or `.save`, suddenly your business logic depends on the database shape, rather than on contracts. What if your database shape is wrong? What if your database shape needs to change?
The goal of good architecture is to be resilient to change, or to even _postpone the choice_ to a moment in time where you have more data to make the right one.
The safest way is: query with activerecord, map to plain ruby objects and discard the activerecord ones immediately.
This will also help you discover the entities you didn't know about, for example, if you have a table of users identified by email, that's the "User" table in Rails. However, let's say that you take only a subset of that table (with select), such as "Full Name" and "Email": this could represent something different. A Newsletter::Subscriber for example.
And the newsletter subscriber entity can be used in the Newsletter::Weekly::Send object, as well as the Newsletter::Unsubscribe. If you realize that this should not have been in the users table in the first place (I have no idea myself), the cost of change is way lower than if you passed an activerecord object around.
I hope this answers some of your questions. There is a lot to say on the topic. I'm happy to chat more on slack or an online videoconference session