erix

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:

  1. data.params.model is CC.Mpesa.Woo.Model, set earlier in the program from constants
  2. query is using import 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

jswanner

struct!(model, date: today, ...)

Also Liked

dimitarvp

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.

Where Next?

Popular in Questions Top

sergio
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
marius95
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
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
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
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
ycv005
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
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
script
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
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

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
siddhant3030
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
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
greenz1
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
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
bsollish-terakeet
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
jason.o
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
nsuchy
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
PeterCarter
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

We're in Beta

About us Mission Statement