defmodule ParrotApi.Meetup do
schema "meetups" do
field :timestamp, :integer
belongs_to :topic, ParrotApi.Topic
timestamps()
end
@required_fields ~w(timestamp)
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields)
|> cast_assoc(:topic, required: true)
|> validate_required(@required_fields)
end
end
But when I do this in my tests:
%Meetup{timestamp: future_time, topic_id: topic.id} |> Repo.insert
it doesn't through an error:
iex(10)> %Meetup{timestamp: 123123} |> Repo.insert
[debug] QUERY OK db=0.3ms
begin []
[debug] QUERY OK db=1.3ms
INSERT INTO "meetups" ("timestamp","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" [123123, {{2017, 4, 13}, {12, 3, 24, 644065}}, {{2017, 4, 13}, {12, 3, 24, 644074}}]
[debug] QUERY OK db=1.6ms
commit []
{:ok,
%ParrotApi.Meetup{__meta__: #Ecto.Schema.Metadata<:loaded, "meetups">, id: 6,
inserted_at: ~N[2017-04-13 12:03:24.644065], timestamp: 123123,
topic: #Ecto.Association.NotLoaded<association :topic is not loaded>,
topic_id: nil, updated_at: ~N[2017-04-13 12:03:24.644074]}}
iex(11)>
How do I ensure that Meetups are only made with a topic_id present??
Meetup.changeset(%Meetup{}, %{timestamp: 123123}) |> Repo.insert
. - Dogbert** (ArgumentError) could not convert the parameter 'timestamp,' into an atom, 'timestamp,' is not a schema field
- bigpotato