Ecto.Associations.HasOne.Proxy
A has_one association.
Create
A new struct of the associated model can be created with struct/2
. The
created struct will have its foreign key set to the primary key of the parent
model.
defmodule Post do
use Ecto.Model
schema "posts" do
has_one :permalink, Permalink
end
end
post = put_primary_key(%Post{}, 42)
struct(post.permalink, []) #=> %Permalink{post_id: 42}
Reflection
Any association module will generate the __assoc__
function that can be
used for runtime introspection of the association.
__assoc__(:loaded, assoc)
- Returns the loaded entities or:not_loaded
;__assoc__(:loaded, value, assoc)
- Sets the loaded entities;__assoc__(:target, assoc)
- Returns the model where the association was defined;__assoc__(:name, assoc)
- Returns the name of the association field on the model;__assoc__(:primary_key, assoc)
- Returns the primary key (used when creating a an model withnew/2
);__assoc__(:primary_key, value, assoc)
- Sets the primary key;__assoc__(:new, name, target)
- Creates a new association with the given name and target;
Summary
get(arg1) | Returns the associated struct. Raises |
loaded?(arg1) | Returns |