Update associations

This commit is contained in:
Logan Hunt 2022-04-06 14:16:29 -06:00
parent 4067339e8c
commit 5ad6bbfce7
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 52B3774857EB24B1
3 changed files with 21 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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