silverdr
Building on the example from another thread:
https://forum.elixirforum.com/t/many-to-many-once-again-extra-data-on-the-join-table/
User has many Chatroom through UsersChatrooms.
Now, I would like to get names and ids of given user’s chatrooms. I surely can do for example:
user = Repo.preload(user, :chatrooms)
but this will load “everything” from both final and join tables, while I’d rather do something like Rails’ pluck or at least select so that only columns I am interested in are queried. What are the “correct” ways of doing this with Ecto associations?
Alternatively already while fetching the user record
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 16 Posts
LostKobrakai
You can use ecto query to select what you need. There’s not really a high level API for that in ecto.
or
silverdr
Ah, so basically this means that one has to build an appropriate query himself. I presume the same would apply if I didn’t have the
Userstruct already and wanted to fetch the user along with its selected associated data, right?dimitarvp
Yep, more or less. And it’s important to remember that Ecto is explicit: it tries very hard not to do things you didn’t ask it to do.
silverdr
TNX.
I can understand and even relate to that on some occasions. On the majority of them though it feels similar to what I felt many years ago when I had to finish application written by somebody else, using “Zend framework”. My main thought and takeaway from that experience was something like “what on Earth do I need this whole Zend framework for, if all I do with it is translating SQL into framework’s syntax, which is going to be then translated back into SQL anyway”. In the majority of cases like the fairly trivial one here, I’d be happy to write a one-liner:
and be done with it, trusting the library to do The Right Thing™, instead of forcing me to think in SQL terms first, and then how to express those in the library’s own syntax/DSL. So yes, there are pros and cons. And yes, there are cases where fine-grained, low-level tuning is important but in lots of typical ones it isn’t. At least IMHO
dimitarvp
I sympathize with the need. You can take it one step further by making helpers for it in your context/domain modules, e.g. with the second coding snippet that @LostKobrakai showed you like so:
And it becomes intuitive to read the consuming modules when they do stuff like
Users.with_chatroom_names(user) |> do_other_stuff()…IMO anyway; naming is hard).I was a Rails dev too, long ago (for 6.5 years). I still sometimes miss bits and pieces from it but as time went by I gravitated towards preferring to get my hands a little more dirty for the added peace of mind that when looking at the app’s code then what I see is exactly what I get.
But again, if you find that clunky, nothing stops you from emulating a few favourite Rails-like idioms in your projects, or even spin them off to internal company libraries (I’ve done so in the past).
silverdr
I worked with numerous languages/frameworks but of all of them my relationship with Elixir/Phoenix is so far probably the most “intense” and of “love-hate” kind. I believe thanks to this I have a glimpse of what people with bipolar disorder feel
LostKobrakai
Ecto is far more geared towards giving developers the ability to deal with the sql. It’s not an ORM and doesn’t try to be. One could probably built one on top of ecto. I can see this not being great when trying to get things of the ground, but it’s really great once you need to maintain that thing flying.
silverdr
That’s what I keep telling myself
and others
And – BTW – I think this is an excellent analogy. That’s exactly how I feel: V1 reached[1] long time ago but Vr still not even close, despite the end of runway uncomfortably well in sight.
[1] - rewriting it in something else no longer an option, especially that it was me who insisted on biting the bullet and diving into this brave new world
sodapopcan
Well if that ain’t some hyperbole yet I know exactly what you mean
My super hot take as a fellow long-time rails developer is that the sooner you forget all conveniences of rails and any other OO-based web framework you’ve used, the sooner you will find happiness. You will maybe even start to feel that you are in a better place! By your last post, it does seem like you are getting there
Another thing to keep in mind is that Ecto is very lightweight compared to a memory hog like ActiveRecord. I would say that the true selling point of
.pluckis that it bypasses ActiveRecord object creation and returns your results in language primitives. In the case of Ecto, this is all it knows how to do!!!Where I’m going with this is that say you already have something general like this:
Then it’s not the end of the world to do this:
…especially if your
chatroomstable doesn’t have a lot of columns.You could even pull that last line into a helper:
Now you can do:
…and hey, that kinda almost sorta mostly loosely sorta kinda looks like Rails!
Do I recommend doing this? Not realllllly, but maybe? It would be nice for use in
iex. I’m really just sharing where my mind went when I was in your position, especially in regards to unlearning muscle-memory from ORMs and that Ecto is a very different beast in more ways than it makes you deal with SQL more directly.silverdr
The first thing I don’t want to do is I don’t want to query the fields I am not interested in. For that I could use
selectand since on top of that, in the given case I am interested only in values thenpluckgives me both in one goAnyway - thanks for the input. The major takeaway I think is