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
that shows the issue:

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
answer part belongs to an answer and to a question part (the one is

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
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
eager loading them.

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

How could I solve this? I have explained a bit better the problem in the
app’s readme:


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