nallwhy

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

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

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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
ovidiubadita
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jerry
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
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

Other popular topics Top

electic
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
gausby
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...
1207 40082 209
New
saif
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
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

We're in Beta

About us Mission Statement