Nvim

Nvim

What is the advantage of Ecto over Rails ORM?

What is the advantage of Ecto over Rails ORM?

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

Ecto is much more comparable to Hanami’s ORM (hanami-model) than Rails’ ActiveRecord: Both Ecto and hanami-model are based on ideas from Domain Driven Design.

The main advantage of this approach overr ActiveRecord’s is that Ecto Schemas (which are called ‘Entities’ in Hanami by the way), that describe the structure of your datatypes are decoupled from the way they are to be persisted (which are called ‘Repositories’ in both). By decoupling these two concerns, it is a lot easier to re-use and test your entities, as well as keeping them smaller. Also, swapping out one repository for another (for instance for sharding or multi-tenancy applications) is now something that can then be easily done.

The main advantage of the ActiveRecord-way is that it is definitely slightly easier to set up and get up and running with fast. However, in the long run, because for instance it’s reliance on e.g. hooks like before_create or after_save, it is near-impossible to test and maintain your application without strongly coupling everything to the database at all times.

13
Post #4
axelson

axelson

Scenic Core Team

One concrete benefit of the explicit Ecto approach is that it’s very easy to separately query a master vs a replicated database, you just need to use a different repo for each. With ActiveRecord it’s all implicit so you can’t freely switch back and forth.

11
Post #8
PragTob

PragTob

This is the main design difference in my mind.

  • ActiveRecord automagically loads records leading to nasty surprises and n+1 records, ecto is explicit about it
  • similarly in ActiveRecord some methods might go to the databse, some may not… it’s on you to know, in ecto you know when you go to the database (when you call out to Repo)
  • as mentioned by @Qqwy is that in ecto you pick yourself what the attributes are (your schema) whereas ActiveRecord just grabs everything (which can also hit performance if you have huge columns that you rarely need for instance)
  • Ecto is a lot closer to SQL than ActiveRecord - as said before Ecto is not your ORM - which makes translating SQL easier
  • Perhaps my favorite thing abut Ecto are changesets as they are use case oriented, meaning I pick and choose which attributes can change and then which validations I want to run as opposed to ActiveRecord where all validations and callbacks are run all the time by default - I just gave a 45 minute talk detailing this problem and having a look at possible solutions
  • The one thing I’d put on ActiveRecord’s side: It’s much easier to handle in the beginning. As association are automatically loaded you don’t have to worry about preloading. As it is Repo and schema in one it’s easier to write Article.find(1) than Repo.get(Article, 1). In the long run I highly prefer the tradeoffs/implications of all of these on the ecto side. You don’t have to care about what changeset to use because there is no choice.

ActiveRecord often feels like implicit magic that you need a degree in to handle, Ecto is simpler and explicit which is a definitive win.

10
Post #6

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement