在Ecto中添加和删除具有多对多关系的记录

时间:2019-01-08 20:30:21

标签: elixir phoenix-framework ecto

我有2个具有man_to_many关系的ecto模式模型。

schema "Users" do

  many_to_many :locations, Location, join_through: "locations_users"
end

schema "Locations" do
  many_to_many :users, User, join_through: "locations_users"
end

所以在我的User.ex模式模型文件中我应该如何编写要添加和删除用户的函数

def add_location(location) do
  # ???
end

def remove_location(location) do
  # ???
end

在ecto文档中说,在添加/删除关联项之前不必加载所有关联的数据,您只需设置关联即可。但是由于我使用的是多对多,所以我不确定如何设置关联,因为它只是一个联接表。

0 个答案:

没有答案