Question_parts: a N+1 problem when updating a model with a relationship

Hello all.

I have a N+1 problem which I cannot solve. I have a sample app hosted
here
that shows the issue: https://github.com/davidmles/question_parts

This is how it works:

There are 4 models: question, question_part, answer, and answer_part.
A question has N parts, and also N answers. An answer has N parts. And
an
answer part belongs to an answer and to a question part (the one is
answering).

An answer can be a draft or published. A draft does not need to have all
its parts answered, while a published answer does need them answered.

The problem:

When an answer is created, but not all its parts are answered, and I try
to
publish it, validation will fail and the edit form will be shown again.
When showing that edit form after validation failed, there will be a N+1
problem, as answer parts will load the corresponding question parts
without
eager loading them.

I have tried to eager load them, but then the edit form will not show
the
proper error.

How could I solve this? I have explained a bit better the problem in the
app’s readme:
https://github.com/davidmles/question_parts/blob/master/README.md

Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs