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 Responses

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.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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 I...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement