schema "votes" do
belongs_to :entry, Entry
belongs_to :account, Account
timestamps(type: :utc_datetime)
end
schema "accounts" do
field :email, :string
field :password, :string, virtual: true
field :encrypted_password, :string
has_many :votes, Vote
timestamps(type: :utc_datetime)
end
schema "entries" do
field :author, :string
field :author_id, :integer
field :content, :string
field :entry_id, :integer
field :entry_timestamp, :string
field :favorite_count, :integer
field :topic_uri, :string
has_many :votes, Vote
timestamps(type: :utc_datetime)
end
I have the three schemas above and I am trying to create a Vote
using cast_assoc
and put_assoc
.
Below is a changeset function for a Vote
. The problem is I’ve got a unique index on entry_id
(it’s not the primary key) so when I pass the entry_id
if an entry with that id exists than cast_assoc
throws unique constraint validation error.
How can I make it so that cast_assoc
actually updates as well on entry_id
or find by entry_id
maybe?
def changeset(vote, %{account: account, entry: entry} = attrs) do
vote
|> cast(attrs, [])
|> cast_assoc(:entry, with: &DertGG.Entries.Entry.changeset/2)
|> put_assoc(:account, account)
end