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.

Last Post!

smathy

smathy

Not to mention organizations who use “tableau”-type reporting tools to create their own business reports. Keeping proxy values in sync between your code and not-your-code is a massive PITA.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
baxterw3b
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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

We're in Beta

About us Mission Statement