Athunnea

Athunnea

Validate_inclusion in db data, thoughts on refactoring

Hi there!

I have an Ecto Changeset, that uses validate_inclusion against quite a few lists of values (country codes, packaging types, categories, etc). Some of these lists are in the database. Requesting the db from the changeset would be inefficient in case of batch inserts/updates.

I have been preloading these lists and sending to the changeset, so it would be one call to db on each batch of updates/inserts, instead of one per each row. Plus it feels more “pure” in terms of functions.

Now it becomes cumbersome. I’m considering to switch to calls directly from the changeset and using cachex as a proxy.

Current code:

def create_batch(list_of_params) do
   lists_of_values = App.Lists.get_lists()

   Enum.map(list_of_params, fn params ->
      changeset(struct, params, lists_of_values) |> Repo.insert
   end
end

def changeset(struct, params, lists_of_values)
   struct
   ...
   |> validate_inclusion(:field, lists_of_values[:package_codes])
end

Potential refactoring

def create_batch(list_of_params) do
   Enum.map(list_of_params, fn params ->
      changeset(struct, params) |> Repo.insert
   end
end

def changeset(struct, params)
   struct
   ...
   |> validate_inclusion(:field, App.CachedLists.get(:package_codes))
end

I have some concerns about potential downsides. Would appreciate an advice.

  • Keep sending lists of values to changeset
  • Request lists of values directly from changeset
0 voters

First Post!

dimitarvp

dimitarvp

I voted for your first option because I really don’t understand how it is cumbersome?

Last Post!

ityonemo

ityonemo

Option 3: refactor into an association?

Where Next?

Popular in Questions Top

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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lanycrost
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement