タイトルは簡単に言えば問題です。私が使用している単純化されたモデルは次のとおりです。
class Test
include Mongoid::Document
field :name, :type => String
embeds_many :questions
attr_accessible :name, :questions
end
class Question
include Mongoid::Document
field :text, :type => String
embedded_in :test
has_many :answers
attr_accessible :text, :test, answers
end
class Answer
include Mongoid::Document
field :value, :type => Integer
belongs_to :question
belongs_to :user
attr_accessible :value, :question, :user
end
class User
include Mongoid::Document
has_many :answers
attr_accessible :answers
end
最小限のデータベース クエリで、ユーザーのテストで未回答の質問をすべて取得できるようにしたいと考えています。
これは私がこれまでに考えた最善の解決策です
answered = []
user.answers.each {|a| answered << a.question_id}
test.questions.where(:_id.nin => answered).length
これに関するヘルプは大歓迎です。