Hello folks,
I’m trying to build an application similar to Goodreads, and as such I’ve run into many_to_many relationships, such as an author having multiple books, and a book having multiple offers. I’m beginning to wrap my head around using put_assoc, and I was just wondering if there is a preferred place to actually set the associations. So far I’ve considered putting it into my changeset directly like so
def changeset(book, attrs) do
required_fields = [:title, :description, :isbn_13, :slug]
optional_fields = []
book
|> cast(attrs, required_fields ++ optional_fields)
|> validate_required(required_fields)
|> put_assoc(:authors, attrs.authors)
end
or doing it within the context that I am using to work with all the various objects in the data model.
def create_book(attrs) do
%Book{}
|> Book.changeset(attrs)
|> Ecto.changeset.put_assoc(:authors, attrs.authors)
|> Repo.insert()
end
Is there any advantage to either approach, or an idiomatic way in Elixir?
Thanks in advance.






















