Ecto中的多对多关系

时间:2015-10-02 03:25:48

标签: elixir phoenix-framework ecto

我有一个用户模型和一个聊天模型。直观地,多个人将在任何时间属于同一个聊天组,每个人可以拥有许多聊天组。因此,聊天组必须属于多个user_id

我的聊天组和用户的架构是:

schema "chatGroups" do
    field :name, :string
    has_many :messages, Message
    belongs_to :user, User

    timestamps
end

schema "users" do
    field :name, :string
    has_many :chatGroups, ChatGroup

    timestamps
end

有任何建议如何处理?

2 个答案:

答案 0 :(得分:35)

Ecto通过关系支持has_many/3。这涉及在聊天组和用户之间创建一个中间表。

您可以使用以下架构执行此操作:

chat_group.ex:

schema "chat_groups" do
  has_many :chat_group_users, MyApp.ChatGroupUser
  has_many :users, through: [:chat_group_users, :user]
end

chat_group_user.ex:

schema "chat_group_users" do
  belongs_to :chat_group, MyApp.ChatGroup
  belongs_to :user, MyApp.User
end

您也可以通过其他方式进行关联:

user.ex:

schema "users" do
  has_many :chat_group_users, MyApp.ChatGroupUsers
  has_many :chats, through: [:chat_group_users, :chat]
end

这使您可以执行以下操作:

Repo.get(Chat, 1) |> Repo.preload(:users)

这将获取聊天模型的用户,并使用值填充:user键。

答案 1 :(得分:32)

这是一个老问题,以前接受的答案不再是事实上的方式。

Ecto现在支持HABTM或多对多关联。

https://hexdocs.pm/ecto/Ecto.Schema.html#many_to_many/3

<!DOCTYPE html>
<html ng-app="confusionApp">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="dishDetailController as ctrl">
    <div ng-repeat-start="d in ctrl.dishes" ng-bind="d.name"></div><br>
    <div style="margin-left: 10px" ng-repeat-end ng-repeat="c in d.comments">
      <span ng-bind="c.rating + ' stars'"></span><br>
      <span ng-bind="c.comment"></span><br>
      <span>{{c.author}}, {{ c.date | date: 'MMM. dd,yyyy' }}</span><p>
    </div>
</body>

</html>
相关问题