Assoc v0.1.1 Assoc.Schema behaviour View Source
Usage
defmodule MyApp.User do
use MyApp.Schema
use Assoc.Schema, repo: MyApp.Repo
schema "users" do
field :email, :string
field :name, :string
has_many :user_roles, MyApp.UserRole, on_delete: :delete_all, on_replace: :delete
timestamps()
end
def updatable_associations, do: [
user_roles: MyApp.UserRole
]
def changeset(struct, params \ %{}) do
struct
|> cast(params, [:email, :name])
|> validate_required([:email])
end
end
Key points:
- The
use Assoc.Schema
line should come afteruse MyApp.Schema
. - Pass the app's Repo into
use Assoc.Schema, repo: MyApp.Repo
Define a
updatable_associations
function. For each updatable association:- The
key
should be the association name - The
value
should be the association schema module
- The
The standard
changeset
function does not change.- Include all the standard code for updating struct values (e.g. name, email) in
changeset
- The library will create and use a separate
associations_changeset
to manage the associations
- Include all the standard code for updating struct values (e.g. name, email) in
Link to this section Summary
Link to this section Callbacks
Link to this callback
updatable_associations()
View Source
updatable_associations()
View Source
updatable_associations() :: List.t()
updatable_associations() :: List.t()