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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
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
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
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
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...
New
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
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
- #forms
- #api
- #metaprogramming
- #hex
- #security









