Я хочу сделать «ежедневный обед для голосования» для людей здесь, на работе, используя Phoenix Framework. Модель, которую я думал о создании, была Votation
, с каждой версией, содержащей много встроенных схем Restaurants
(Прочитайте here для информации о встроенных схемах). Модель выглядит так:Phoenix Framework: limit Вставить в базу данных по 1 в день
defmodule WhereToLunch.Votation do
use WhereToLunch.Web, :model
schema "votations" do
embeds_many :restaurants, Restaurant
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [])
|> validate_required([])
|> #TODO: Is it !was_votation_inserted_today() ??
end
@doc """
Returns `true` if a `Votation` object was already inserted in the database
on the same day the function is called. Returns false otherwise.
"""
def was_votation_inserted_today() do
#TODO: How to check if a object was already inserted in the database
# on the same day the function is called?
end
end
defmodule WhereToLunch.Restaurant do
use Ecto.Model
embedded_schema do
field :name, :string
field :votes, :integer, default: 0
end
end
То, что я хочу сделать, это не позволит более чем один Insert
в таблице where_to_launch.votations
в день. Каков наилучший подход для этого?
Воспользуйтесь уникальным индексом ваших баз данных в поле даты. –