Seeding relationships with `seed_generator`?

Is it possible to seed relationships inline with seed_generator? e.g. something along the lines of
def blog_post(attrs) do
seed_generator({MyApp.BlogPost, %{}}, overrides: attrs)
end
def blog_post(attrs) do
seed_generator({MyApp.BlogPost, %{}}, overrides: attrs)
end
# in test
owners = generate_many(author())
generate(blog_post(authors: authors))
# in test
owners = generate_many(author())
generate(blog_post(authors: authors))
Seems that belongs_to works OK using thing_id but I'm trying to seed some many_to_manys which doesn't seem to work as I'd expect. The docs I'm finding seem fairly sparse on this sort of thing so I'm really feeling my way around here 😅
2 Replies
Jesse Williams
Jesse WilliamsOP•7d ago
Seems like in general, using relationships by name isn't working as I'd expect. using IDs works for belongs_to but that seems to be it For example, if I refactor the above to manually seed the join table record, I get something like
authors = generate_many(author(), 3)
blog_post = generate(blog_post())
# doesn't work (complains about author_id + blog_post_id not existing)
_relationships = Enum.map(authors, &generate(blog_post_author(author: &1, blog_post: blog_post)))
# does work, but feels clunkier imo
_relationships = Enum.map(authors, &generate(blog_post_author(author_id: &1.id, blog_post_id: blog_post.id)))
authors = generate_many(author(), 3)
blog_post = generate(blog_post())
# doesn't work (complains about author_id + blog_post_id not existing)
_relationships = Enum.map(authors, &generate(blog_post_author(author: &1, blog_post: blog_post)))
# does work, but feels clunkier imo
_relationships = Enum.map(authors, &generate(blog_post_author(author_id: &1.id, blog_post_id: blog_post.id)))
What's interesting is that Ash.Generator.seed! seems to handle relationships by name great, it's just when you do it via seed_generator that the relationship names don't work 🤔
ZachDaniel
ZachDaniel•7d ago
Interesting. Thats probably a bug.

Did you find this page helpful?