Ecto: select 记录 inserted_at 特定日期
Ecto: select records inserted_at a specific date
我有一个很简单的Ecto model:
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :name, :string
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \ %{}) do
struct
|> cast(params, [])
|> validate_required([])
end
end
迁移:
defmodule MyApp.Repo.Migrations.CreateUser do
use Ecto.Migration
def down do
drop table(:users)
end
def change do
drop_if_exists table(:users)
create table(:users) do
add :name, :string
timestamps()
end
end
end
我想 select 所有 users
的 inserted_at
特定日期(例如,今天)。最好的方法是什么?
我不确定如何单独通过 Ecto 来做到这一点。所以这个答案将特定于 SQL。我只在 PostreSQL.
上试过这个
Attendance.Repo.all(from s in Something,
where: fragment("date(inserted_at) = ?", ^~D[2017-02-06]))
我有一个很简单的Ecto model:
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :name, :string
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \ %{}) do
struct
|> cast(params, [])
|> validate_required([])
end
end
迁移:
defmodule MyApp.Repo.Migrations.CreateUser do
use Ecto.Migration
def down do
drop table(:users)
end
def change do
drop_if_exists table(:users)
create table(:users) do
add :name, :string
timestamps()
end
end
end
我想 select 所有 users
的 inserted_at
特定日期(例如,今天)。最好的方法是什么?
我不确定如何单独通过 Ecto 来做到这一点。所以这个答案将特定于 SQL。我只在 PostreSQL.
上试过这个Attendance.Repo.all(from s in Something,
where: fragment("date(inserted_at) = ?", ^~D[2017-02-06]))