mlen

mlen

Ecto.Enum select underlying id

I seem to be stuck on some basic Ecto logic.

my Ecto version is 3.7.1

Given some schema:

schema :users do
  field :name, string
  field :zone, Ecto.Enum, values: ["Zone A": 1, "Zone B": 2, "Zone A and B": 3]  # stored as integer in db
  ..
end

FYI: I don’t want to use composite types of :array, neither :string type for the zone.

Then when I do query the data:

# simplified for brevity
from(
  u in Users,
  select: [:id, :name, :zone],
  where: [name: ^some_name]
)
|> Repo.all()

Each user object’s zone is shown as atom:

%MyApp.Users.User{
  __meta__: ...,
  id: 1,
  name: "User A",
  zone: :"Zone A"  # I need this as integer
}

How do I make Ecto return the corresponding ids for zone field?

The reason I need that is later in the code I’m trying to create two list of users (with one db query), like so:

users  = Users.get_all_active_users()
users_zone_a = Enum.reject(users, fn u -> u.zone == 2 end)
users_zone_b = Enum.reject(users, fn u -> u.zone == 1 end)

Any help greatly appreciated :slight_smile:

Marked As Solved

LostKobrakai

LostKobrakai

The whole idea of Ecto.Enum is to not spread the persisted value (the integers in your case) all over the codebase. You’re expected to use the atoms wherever you interact with the field, not the integers. Otherwise you could just use an :integer field to begin with.

But also there’s Ecto.Enum.mappings if you want to map between the representations.

Also Liked

krasenyp

krasenyp

That’s the idea of the enum in this case, to separate the presented value from the persisted one. What I would advise is to create a helper module which can encode and decode integers to and from :"Zone A", :"Zone B" and so on. You should do the encoding/decoding at the “points” where your code interacts with the external word e.g. HTTP endpoints, database. This would allow you to work only with meaningful values and your comparisons will work.

Where Next?

Popular in Questions 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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
nsuchy
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
bsollish-terakeet
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
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
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

We're in Beta

About us Mission Statement