dipth

dipth

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

Showing Posts 4 to 1

dipth

dipth OP

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…

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 OP

[info] POST /profile
[debug] QUERY OK source=“users” db=1.0ms decode=0.1ms
SELECT … FROM “users” AS u0 WHERE (u0.“id” = $1) [1]
[debug] Processing by Helheim.ProfileController.update/2
Parameters: %{“_csrf_token” => “aCEAUVpdQ0IOZBsZAyJnE0AKBTI9JgAAGp34nir096OsZN4k73TbyA==”, “_method” => “put”, “_utf8” => “✓”, “user” => %{“birthday” => %{“day” => “31”, “month” => “12”, “year” => “2017”}}}
Pipelines: [:browser, :browser_auth]
[info] Sent 200 in 6ms

It appears that there is some sort of implicit validation taking place since f.errors[:birthday] in the template returns “is invalid”

I have also tried using Timex like so:

field :birthday, Timex.Ecto.DateTime

which just gives an error:

no function clause matching in Timex.Protocol.Tuple.to_datetime/2

Trying with a Timex date field instead:

field :birthday, Timex.Ecto.Date

gives another error:

could not encode date/time: {2017, 12, 31}

radar

radar

What do the logs show when that action is hit?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews