帮助行动MAIL

时间:2011-07-05 07:03:26

标签: ruby-on-rails

require 'config/environment'

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

inquiry.each do |i|   
    question          = i.question 
    user              = User.find(:first, :conditions => ["id = ?", question.user_id])

    Notifier.deliver_deadline_notification(inquiry, user, question)
end

我有inquiry(关系表)与is_answered字段(例如)。

我需要什么?我需要在我的问题NOT answered上发送is_answered = 0的电子邮件给我们。所以现在以这种方式工作:我收到了2封电子邮件(因为我在数据库中有1个问题,2个用户没有得到答复):

id | question_id | is_answered
14 |     11      |     0
24 |     11      |     0

所以,我需要接收ONLY ONE EMAIL而不是两个!在电子邮件中,我想写一些关于问题的统计数据。但我只需要一个电子邮件!我怎么可能这样做?

谢谢你!

------------------ UPD -----------------

model/notifier.rb
  def deadline_notification(inquiry, user, question, respondent)
     recipients   user.email
     from         "hey@hey.com"
     subject      "Finished"
     content_type "text/html"
     body(:question => question.text, :respondent => respondent.email)
  end

model/inquiry

class Inquiry < ActiveRecord::Base
  belongs_to  :question
  belongs_to  :respondent
  has_one     :answer, :dependent => :destroy

model/question

class Question < ActiveRecord::Base
  has_many :inquiries, :dependent => :destroy
  has_many :answers, :through => :inquiries, :dependent => :destroy
  belongs_to :user
end

model/respondent

class Respondent < ActiveRecord::Base 
  has_many    :inquiries, :dependent => :destroy
  has_many    :questions, :through => :inquiries
  belongs_to  :user
end

model/user

class User < ActiveRecord::Base
  has_many :questions
  has_many :respondents

TODO:查找is_answered = 0的所有inquries(例如我有100个受访者,70个人被回答)并发送ME电子邮件(用户,添加问题确定,(它正在工作))70位受访者回答(AND谁完全)谁没有回答。只是发送一封电子邮件!

PS - 现在我收到了30封电子邮件(没有回复),但我认为这是错误的:D

2 个答案:

答案 0 :(得分:0)

听起来你在这里要做的就是找到所有没有回答问题的用户 - 就像

`User.all(:includes=>[:questions, :inquiries], :conditions=>["is_answered=?",0])

然后您可以将集合(查询,问题)传递给视图并迭代以列出您的统计信息

希望我明确这一点 - 如果不是,如果您在模型上发布关联以及您正在使用邮件的视图,我可能会做得更好......

答案 1 :(得分:0)

好的,我想看看你现在在做什么! <怎么样

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

#get all users who have unanswered inquiries and build a hash {:user=>[inquiries]}
user_hash= inquiries.inject({})(|uh,i| uh.key?(i.question.user) ? uh[i.question.user] << i : uh[i.question.user]=>[i]
uh)


#iterate thro the users and send a mail to each ...
user_hash.each do |user, inquiries|   
#obviously your existing view won't work here - you need to iterate thro inquiries
Notifier.deliver_deadline_notification(user, inquiries)
end