我希望标题中的描述足够说明性,但是要进一步阐述,具有这样的模型(写在我的头上,请谅解,如果有任何轻微的语法错误,等等):>
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
现在:我已经以可以创建喜欢的方式设置所有内容,没问题。但是我对如何限制用户一无所知,因此每个评论最多只能有一个这样的用户。
如果这可以帮助任何人向我解释它,那么我来自Rails的背景并且只是开始探索Elixir / Phoenix的功能范例。
答案 0 :(得分:1)
在数据库级别的(user_id, comment_id)
表中定义唯一键likes
。
BTW,在Rails和世界上存在的任何其他编程语言/框架中,都可以通过相同的方式解决此问题。