nallwhy
Append fields using select query on Ecto
I have a schema and it has many fields and one belongs_to association.
I want to query this model with one field of associated model like below.
def MyModel do
schema "my_models" do
field(:field0, :string)
field(:field1, :string)
field(:field2, :string)
field(:field3, :string)
field(:field4, :string)
field(:field5, :string)
field(:field6, :string)
field(:field7, :string)
field(:field8, :string)
field(:other_model_id)
end
end
def list() do
MyModel
|> join(:inner, [m], o in OtherModel, m.other_model_id == o.id)
|> select([m, o], %{field0: m.field0, field1: m.field1, ... other_model_value: o.value})
end
But I don’t want to repeat field0, field1, …
Is there a better way to do this?
Thanks!
Marked As Solved
nallwhy
I found a way to do it.
def MyModel do
schema "my_models" do
field(:field0, :string)
...
field(:field8, :string)
field(:other_model_id, :integer)
# Add a virtual field
field(:other_model_value, :integer, virtual: true)
end
end
def list() do
MyModel
|> join(:inner, [m], o in OtherModel, m.other_model_id == o.id)
|> select([m, o], %{m | other_model_value: o.value})
end
2
Popular in Questions
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
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
Other popular topics
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









