有关activerecord的问题

时间:2011-07-01 09:00:56

标签: ruby-on-rails ruby

我有5张桌子,他们之间有关系。万事如意,完美!但我试图制作脚本,而不是在查询表字段中is_answered = 0,所以我找到受访者(通过问题表中的respondent_id)并发送给他们的信 但 我有错误!

我有这段代码:

inquiry       = Inquiry.find(:all, :conditions => ["is_answered = 0"])
question      = inquiry.question 
respondents   = Respondent.find(:all, :conditions => ["id = (?)", question.user_id])

respondents.each do |r|
  Notifier.deliver_user_notification(inquiry)
end

当我输入ruby blah.rb时,我收到此错误:

undefined method `question' for #<Array:0x7f646c82b568>

我的错误是什么?

PS - Inquiry表格id, question_id, respondent_i d)questionsanswers之间的关系表。 PSS - 与Respondent table相关的Inquiry

1 个答案:

答案 0 :(得分:4)

问题是您有更多查询,因此以下内容会返回一个数组。

Inquiry.find(:all, :conditions => ["is_answered = 0"])

尝试以下方法,但要注意它的查询次数,因为可能会进行优化:

inquiry       = Inquiry.find(:all, :conditions => ["is_answered = 0"])
inquiry.each do |i|
    question      = i.question
    respondents   = Respondent.find(:all, :conditions => ["id = (?)", question.user_id])

    respondents.each do |r|
      Notifier.deliver_user_notification(inquiry)
    end
end