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.

Last Post!

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

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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