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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
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 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
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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









