3
\$\begingroup\$

I am building an application messaging system. I am planning on using channels for real-time notifications but I was wondering if this schema makes sense. I am looking for any constructive criticism and problems that might arise.

message.ex

defmodule Trophus.Message do
  use Trophus.Web, :model

  schema "messages" do
    belongs_to :conversation, Trophus.Conversation
    field :body, :string
    timestamps
  end

  @required_fields ~w(body conversation)
  @optional_fields ~w()

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end
end

conversation.ex

defmodule Trophus.Conversation do
  use Trophus.Web, :model

  schema "conversations" do
    has_many :messages, Message
    belongs_to :initiator, User, foreign_key: :user_id
    belongs_to :receiver, User, foreign_key: :user_id
    timestamps
  end

  @required_fields ~w(initiator receiver)
  @optional_fields ~w(messages)

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end
end
\$\endgroup\$

migrated from stackoverflow.com Jul 8 '15 at 17:02

This question came from our site for professional and enthusiast programmers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.