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
struct!(model, date: today, ...)
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
Popular in Questions
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
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
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
can someone please explain to me how Enum.reduce works with maps
New
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
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New







