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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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