class QuestionGroup < ActiveRecord::Base
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :question_group
has_many :question_answers
has_many :question_users_answers, :through => :question_answers, :source => :user_question_answers
def self.questions_without_answers(user_id)
select {|q| q.question_users_answers.where(:user_id=>user_id).empty?}
end
end
class QuestionAnswer < ActiveRecord::Base
belongs_to :question
has_many :user_question_answers
end
ユーザーの回答がない場合は、すべての質問を検索する必要があります
しかし、特定のユーザーに対して、questions_without_answers が存在するすべての QuestionGroups を見つけるにはどうすればよいでしょうか?
PS: 未回答のすべての質問と、これらの質問を所有するすべてのグループを検索する必要があります。 findまたはnamed-scopeで実行できますか?
更新しました:
def self.groups_without_answers(user_id)
questions_ids = Question.questions_without_answers(user_id).map {|q| q.id}
all(:conditions => "id in (select distinct question_group_id from questions where id in (#{questions_ids.join(',')}))")
end
しかし、それは良くないと思いますか、それとも間違っているのでしょうか?