sushilbansal

sushilbansal

Ecto Enum display issue

Hi,
I am trying to use Ecto.Enum as following:

 schema "demographics" do
    field :gender, Ecto.Enum,
      values: [
        male: "Male",
        female: "Female",
        other: "Other",
        prefer_not_to_say: "Prefer not to say"
      ]

    field :year_of_birth, :integer
    timestamps(type: :utc_datetime)
  end
  # function to get all the gender options - will be used with select input component
  def get_gender_options() do
    Ecto.Enum.dump_values(__MODULE__, :gender)
  end

It is working fine; it is saving in DB as string (e.g. “Prefer not to say”). Issue occurs when i fetch the db record and display on LiveView. It displays gender as: prefer_not_to_say

How i can get LV to display “Prefer not to say”.

One way i can think of is:

 schema "demographics" do
    field :gender, Ecto.Enum,
      values: [
        :Male,
        :Female,
        :Other,
        :"Prefer not to say"
      ]

    field :year_of_birth, :integer
    timestamps(type: :utc_datetime)
  end

Is there any other way?

Most Liked

LostKobrakai

LostKobrakai

Generally I’d argue that your db representation should be separate from your frontend representation. Just because you store :male runtime value as "Male" in the database doesn’t mean your frontend should render :male as "Male" (applies the same the other direction).

But if you really want to reuse that mapping you can get to it using Ecto.Enum.mappings(schema, :gender).

champeric

champeric

I know some people don’t like using them, but it’s way better in my opinion to use an ENUM if you’re using PostgreSQL. It uses the same space as an int, but it will be readable and better in terms of data integrity (you can’t have a value that doesn’t exist).

There are some caveats, but I never had any problems with them.

CREATE TYPE gender AS ENUM ('male', 'female', 'other');

LostKobrakai

LostKobrakai

Yeah, have two separate mappings. One from atom to frontend string, which can then potentially handle things like localization and another one (the Ecto.Enum one) for atom → db string.

Hermanverschooten

Hermanverschooten

Chiming in, I prefer to use integers in the db, they take less space, and since Ecto returns the atoms in both cases.

smathy

smathy

It’s definitely not tedious to add a value, that’s simple and has been available since pg 9.1. Renaming has been available since 10 which alleviates most of the need for deleting, but yes - deleting is a bit tedious, but IMHO appropriately so - you have other issues if you’re needing to delete enum values more than “almost never” :slight_smile:

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
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
joeerl
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 Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
joeerl
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

We're in Beta

About us Mission Statement