DEV Community

Simple Repository Pattern for Ruby on Rails

Cherry Ramatis on June 13, 2023

What is the repository pattern? (In simple terms) In the famous book "Patterns of Enterprise Application Architecture", Martin Fowler de...
Collapse
 
erick_tmr profile image
Erick Takeshi

Active Records is already an abstraction on top of the DB, it has a lot of more convenience methods because of the nature of its pattern, but, at a certain degree we could already treat our models as repositories.

IMHO, I would see more fit if we use another ORM library (like sqlize), because it is lacking a lot of stuff that ActiveRecord has.

Collapse
 
cherryramatis profile image
Cherry Ramatis

Yeah, certainly! I would still use repository pattern on active record projects, just because it's easier to isolate behavior instead of using the "fat model" philosophy that we have been using.

That said, I agree that this pattern could be used with something like sqlize, sequel or ROM

Recently I had a use case at work that I needed to create a v2 route using a different database (MongoDB) and since I was already using repository pattern, it was just a matter of creating the new repository and copying the controller method (because all method names remain the same)