I hope the description in the title is explanatory enough, but to elaborate a bit more, having models like this (written off the top of my head, please forgive if there are any slight syntax errors, etc.):
defmodule MyApp.Like do
use Ecto.Schema
import Ecto.Changeset
schema "likes" do
belongs_to :user, MyApp.User
belongs_to :comment, MyApp.Comment
timestamps()
end
end
defmodule MyApp.Comment do
use Ecto.Schema
import Ecto.Changeset
schema "comments" do
belongs_to :user, MyApp.User
has_many :likes, MyApp.Likes
timestamps()
end
end
defmodule MyApp.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
has_many :comments, MyApp.Comment
has_many :likes, MyApp.Like
timestamps()
end
end
Now: I have set everything up in a way that it's possible to create likes, no problem. But I don't quite have an idea of how to go about limiting a user, so that he/she could only have a maximum of one like per comment.
If this might help anyone in explaining it to me, I'm coming from Rails background and have only started exploring functional paradigms of Elixir/Phoenix.
Define a unique key (user_id, comment_id)
in likes
table on the database level.
BTW, in Rails and in any other programming language / framework existing in the world, this problem is being solved in the same way.