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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement