Seeding multiple resources with inter-relationships

How do I do something like this in Ash? User and Post are Ash resources where a User has many posts. It doesn't seem to work as it does in Ecto.
Repo.insert! %User{
name: "John",
posts: [%Post{
text: "First post"},
%Post{
text: "Second post"}]
}
Repo.insert! %User{
name: "John",
posts: [%Post{
text: "First post"},
%Post{
text: "Second post"}]
}
2 Replies
ZachDaniel
ZachDaniel3y ago
If you're talking about doing actual data seeds, you can use Ash.Seed to do pretty much exactly that. But if you're building out actions for your application, you'd use things like manage_relationship, or custom behavior in an action of some other kind, to do the relationship changes.
create :create do
argument :posts, {:array, :map}

change manage_relationship(:posts, type: :create)
end
create :create do
argument :posts, {:array, :map}

change manage_relationship(:posts, type: :create)
end
and then
User
|> Ash.Changeset.for_create(%{name: "John", posts: [%{text: "First Post"}, %{text: "Second Post"}]})
User
|> Ash.Changeset.for_create(%{name: "John", posts: [%{text: "First Post"}, %{text: "Second Post"}]})
But IIRC, Ash.Seed.seed!(%User{....}) ought to behave the way you want
Jason
JasonOP3y ago
Awesome. Thank you, Zach!!

Did you find this page helpful?