From 5ad6bbfce753e048a4f866ad6324f4b4d6f16618 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Wed, 6 Apr 2022 14:16:29 -0600 Subject: [PATCH] Update associations --- lib/aggiedit/accounts/user.ex | 1 + lib/aggiedit/rooms/post.ex | 22 +++++++++++++++++++--- lib/aggiedit/rooms/room.ex | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/aggiedit/accounts/user.ex b/lib/aggiedit/accounts/user.ex index aeb37ed..65c2463 100644 --- a/lib/aggiedit/accounts/user.ex +++ b/lib/aggiedit/accounts/user.ex @@ -13,6 +13,7 @@ defmodule Aggiedit.Accounts.User do field :role, Ecto.Enum, values: [:user, :admin], default: :user belongs_to :room, Room, on_replace: :update + has_many :posts, Aggiedit.Rooms.Post timestamps() end diff --git a/lib/aggiedit/rooms/post.ex b/lib/aggiedit/rooms/post.ex index c6ca24a..ee9450d 100644 --- a/lib/aggiedit/rooms/post.ex +++ b/lib/aggiedit/rooms/post.ex @@ -5,9 +5,10 @@ defmodule Aggiedit.Rooms.Post do schema "posts" do field :body, :string field :title, :string - field :user_id, :id - field :upload_id, :id - field :room_id, :id + + belongs_to :room, Aggiedit.Rooms.Room + belongs_to :user, Aggiedit.Accounts.User + belongs_to :upload, Aggiedit.Uploads.Upload timestamps() end @@ -18,4 +19,19 @@ defmodule Aggiedit.Rooms.Post do |> cast(attrs, [:title, :body]) |> validate_required([:title, :body]) end + + def change_user(post, user) do + change(post) + |> put_assoc(:user, user) + end + + def change_upload(post, upload) do + change(post) + |> put_assoc(:upload, upload) + end + + def change_room(post, room) do + change(post) + |> put_assoc(:room, room) + end end diff --git a/lib/aggiedit/rooms/room.ex b/lib/aggiedit/rooms/room.ex index 24f1b8a..2a23555 100644 --- a/lib/aggiedit/rooms/room.ex +++ b/lib/aggiedit/rooms/room.ex @@ -6,6 +6,7 @@ defmodule Aggiedit.Rooms.Room do field :domain, :string has_many :users, Aggiedit.Accounts.User + has_many :posts, Aggiedit.Rooms.Post timestamps() end