What is the advantage of Ecto over Rails ORM?
Ecto is written in elixir and it would be really hard to use active record in an elixir project since active record is written in ruby .
Yes but what is better in ecto that is not in rails orm?
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.
I highly recommend âWhatâs new in Ecto 2.1â free ebook. The content is golden and still very much actual (70+ pages of highly practical examples). JosĂ© goes over this in the âEcto is not your ORMâ chapter.
http://pages.plataformatec.com.br/ebook-whats-new-in-ecto-2-0
Programming Ecto book also discusses this.
Ecto is explicit in its approach while the others are implicit. What this means is that you can more easily shoot yourself in the foot with Railsâs ActiveRecord, Laravelâs Eloquent etc.
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)
thanRepo.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.
Better is relative. Ecto is different than ActiveRecord in a number of ways, though. The most important is that Ecto does not follow the Active Record pattern. Itâs closer to the Unit of Work or Repository pattern. Everything else flows from this difference. Simplifying a bit, ActiveRecord is a map, a true object wrapper, that exposes a data store by mapping it directly to a Ruby class. Ecto doesnât care much about whether or how your schemas relate to the underlying data, or whether you even have a schema or use a changeset. Itâs a broad query language that talks to data structures, not just a data store.
So the advantage is one of developer happiness I suppose. There probably isnât any real benefit to one over the other for most things. Ecto can be said to provide more flexibility, but that would also come with the cost of more responsibility. Rails can feel more âmagicalâ because things (usually) just work but maybe they also work sooner and provide more feedback to the developer. Bottom line: AR and Ecto both solve the same problems in very different ways. The further you go down the road with one or the other, the more divergent they will feel. Whichever of them feels more natural or comfortable for you is probably the one thatâs better.
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.
@sfusato I am new to Elixir and looking for some books to get deep dive into the world of functional programming, domain-driven architecture and Elixir.
Can you please help me to find some free books or learning resources? and the link you share is not working because plataformatec is operational anymore