2016-03-03 98 views
1

我有三种主要模型处理这种情况。简单地说他们的关系是这样的:Rails 4使用嵌套属性多次过滤模型

class Person < ActiveRecord::Base 
    has_many :sessions, dependent: :delete_all, inverse_of: :person 
    has_many :answers, through: :sessions 
end 

class Session < ActiveRecord::Base 
    belongs_to :person, inverse_of: :sessions 
    has_many :answers 
end 

class Answer < ActiveRecord::Base 
    belongs_to :session 
end 

简言之和重点问题,我将总结模型的属性和只专注于应答模式属性,因为它们是我需要做的,只有我需要做的。 答案模式具有以下属性:

# == Schema Information 
# 
# Table name: answers 
# 
# id     :integer   not null, primary key 
# session_id   :integer 
# question_option_id :integer 
# created_at   :datetime   not null 
# updated_at   :datetime   not null 
# 

我想是根据从形式提供question_option_id值过滤人的记录,那里的用户可以与一个或多个答案过滤选项问题选项值。 的值是没有问题的送出,但我没有成功筛选人的记录都试过了,这是最接近我已经与部分成功:我已经能够用价值来过滤

people = Person.all 
people = people.joins(:answers).uniq.where(answers: {question_option_id: some_param}) 

有了这个一个问题的选项,问题是当我试图筛选返回无非就是一个问题的选项,这是我曾尝试:

people = Person.all 
people = people.joins(:answers).uniq.where(answers: {question_option_id: some_question_option_id_param}) 
people = people.joins(:answers).uniq.where(answers: {question_option_id: another_question_option_id_param}) 

的问题是,这些条件下,查询搜索的人有一个答案既有question_option_id值whi ch是不可能的。我试图完成的任务是查询所有拥有的答案中的具有问题选项id的人员。对不起,如果我很难理解,但这是我想要做的,我不知道如果我做的是正确的方法,欢迎任何建议。

回答