I am an experienced dev, but pretty new to elixir. I keep finding myself going back and forth between a couple options for how to construct a changeset that will create a new record with one or more associations.
The following small ecto schema module presents the two options
defmodule MyProject.Quizzes.QuizQuestion do
use Ecto.Schema
import Ecto.Changeset
schema "quiz_questions" do
belongs_to :quiz, MyProject.Quizzes.Quiz
belongs_to :question, MyProject.Questions.Question
timestamps()
end
@doc false
def changeset(quiz_question, attrs) do
quiz_question
|> cast(attrs, [:quiz_id, :question_id])
|> validate_required([:quiz_id, :question_id])
|> foreign_key_constraint(:quiz_id)
|> foreign_key_constraint(:question_id)
end
def create(%MyProject.Quizzes.Quiz{} = quiz, %MyProject.Questions.Question{} = question) do
%__MODULE__{}
|> change()
|> put_assoc(:quiz, quiz)
|> put_assoc(:question, question)
|> foreign_key_constraint(:quiz_id)
|> foreign_key_constraint(:question_id)
end
end
In this example I have three models: Quiz, Question, and a join table QuizQuestions that combines them. I have two functions in the module:
-
change
: this takes in anattrs
map, casts the foreign keyid
, requires them, and adds foreign key constraints -
create
: takes in instances of the related schemas (Question and Quiz), usesput_assoc
to build the association, then adds foreign key constraints
Both of these approaches let me create the new QuizQuestion
struct that is Repo.insert
able, but I still have some questions.
- Is one approach more idiomatic and why?
- Should I prefer one case to the other?
- What are the tradeoffs?
- Is it bad practice to use structs defined in other core modules as arguments to functions (like I did in
create
when I acceptQuiz
andQuestion
structs)?