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
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
Last Post!
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
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
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
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
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
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
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
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
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
- #api
- #forms
- #metaprogramming
- #security
- #hex









