dipth

dipth

Storing date from date_select

How does one go about storing datetime values from a date_select field in the database?

Here is my Ecto model:

defmodule MyApp.User do
  use MyApp.Web, :model

  schema "users" do
    field :birthday, :utc_datetime
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:birthday])
  end
end

Here is my template:

<%= date_select f, :birthday, builder: fn b -> %>
  <%= b.(:day, prompt: "", class: "custom-select") %> /
  <%= b.(:month, prompt: "", class: "custom-select") %> /
  <%= b.(:year, options: Timex.now.year..(Timex.now.year - 100), prompt: "", class: "custom-select") %>
<% end %>

Here is my controller:

def update(conn, %{"user" => user_params}) do
  user = Guardian.Plug.current_resource(conn)
  changeset = User.changeset(user, user_params)
  case Repo.update(changeset) do
    {:ok, _user} ->
      conn
      |> put_flash(:success, gettext("Profile updated!"))
      |> redirect(to: public_profile_path(conn, :show, user))
    {:error, changeset} ->
      conn
      |> render("edit.html", changeset: changeset)
  end
end

I can successfully submit the form, but the birthday is never written to the database

Most Liked

dsissitka

dsissitka

I wonder if you need to use field :birthday, :date.

I wonder if that’s because you’re using :utc_datetime and you aren’t passing in a time.

dipth

dipth

You are right. Changing the column in the database to :date fixes the problem.
I find it weird though that it shouldn’t be possible to store a date value in a datetime field and just have the time-part assumed as midnight.
In this case it isn’t a big problem, but there might be cases where it is important to have the time-part in the database as well as the date in case you want to perform timezone-sensitive checks on the date…

radar

radar

What do the logs show when that action is hit?

Where Next?

Popular in Questions Top

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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement