我有两个模型,即问题和选择,如下所示
class Question < ActiveRecord::Base
has_many :choices, :dependent => :destroy
accepts_nested_attributes_for :choices, :allow_destroy => true
end
class Choice < ActiveRecord::Base
attr_accessible :text
belongs_to :question
end
这里每个问题有 3 个或更多选项,在这 3 个选项中是正确的,它存储在问题表的答案列中。即问题。答案
问题:当我显示所有我想显示选择文本的问题时,这意味着@choice Choice.find(@question.answer)
它会显示正确的答案(即),@choice.text
但它会导致n+1
问题。
我们可以通过一个查询或像...这样的急切加载来实现这一点吗?
看法:
%table
%tr
%th No
%th Question
%th Answer
- @review_questions.each_with_index do |question,index|
%tr
%td= index+1
%td= question.text
%td= question.answer