我有两个模型,响应和问题。一个问题has_many响应,因此每个响应都与question_id相关联。我想查询对象@responses中与响应相对应的问题,但我不确定语法。
@reponses = Response.find([1, 10])
@questions = Question.where(:id => [???])
我最初的想法是这样的,但这种语法是错误的:
@reponses = Response.find([1, 10])
@questions = Question.where(:id => @responses.question_id)
答案 0 :(得分:3)
你非常亲密......试试这个
@questions = Question.where(:id => @responses.map(&:question_id))
但我认为您应该在响应模型中使用范围