David Morales
2014-Jul-09 12:27 UTC
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b3d935f9-1e92-4499-9551-c88274184441%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.