erix
Dynamically reference a module spec
I have a module for each model in my program, and I can query them dynamically:
model = String.to_existing_atom( "Elixir." <> data.params.model )
query = from m in model,
where: m.domain == ^data.payload.domain,
select: %{
:id => m.id,
:date => m.date,
:domain => m.domain
}
result = Cc.MySQL.one(query)
where:
data.params.modelisCC.Mpesa.Woo.Model, set earlier in the program from constantsqueryis usingimport Ecto.Query, only: [from: 2]
This works with no issue.
My question, now, is how to reference a module spec dynamically.
If I statically set the spec:
with {:ok, add} <- Cc.MySQL.insert %CC.Mpesa.Woo.Model{
date: today,
domain: data.payload.domain,
agent: agent,
key: @tools.makeid,
} do
The data will be inserted.
However I get an error when I place model here instead:
with {:ok, add} <- Cc.MySQL.insert %model{
date: today,
domain: data.payload.domain,
agent: agent,
key: @tools.makeid,
} do
Error:
error: expected struct name to be a compile time atom or alias, got: model
│
32 │ with {:ok, add} <- Cc.MySQL.insert %model{
│ ^
How would I insert model as an atom here, so that the spec (schema) is used?
Marked As Solved
jswanner
struct!(model, date: today, ...)
Also Liked
dimitarvp
Don’t use this. Use that instead:
model = Module.concat([data.params.model])
Notice that the function argument is a list with a single item inside.
3
Popular in Questions
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir?
...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
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
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
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
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
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
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
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
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New









