2011-07-05 7 views
0
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 필드 (예 :)가 있습니다.조치 도움말 MAIL

내가 필요한 것? 내 질문에 NOT answered 인 이메일을 보내야합니다 (is_answered = 0). (난 데이터베이스에 나와 있지 않은 1 개 질문 2 사용자가 있기 때문에) 내가 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   "[email protected]" 
    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 : 발견 70 명의 응답자 (정확히 누가 누구인가)와 그렇지 않은 사람 모두가 is_answered = 0 인 경우 (예 : 100 명의 응답자와 70 명의 응답자가 있음) ME 이메일 (사용자가 질문을 추가 했음) 대답했다. 이메일 하나만 보내주세요!

PS - 이제 나는 30 이메일 (나와 있지 않은 사람) 받았지만, 내가 잘못 생각 : D

그것은 질문에 대한 답변하지 않은 모든 사용자를 찾을 수 있습니다 당신이 여기서 뭘하려고 같은 소리

답변

0

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

같은 - 그래서 문의에 그런 다음 뷰에 컬렉션 (문의, 질문)을 통과 할 수 및 통계 내가 만든

희망을 나열 통해 반복이 명확 - 아니 아마도 내가 할 수있는 경우 모델에 연관성을 게시 할 때 더 잘 수행하십시오. 그리고 당신이 메일러에 사용하는보기 ...

+0

도와 주셔서 감사합니다. 내 게시물을 업데이트했습니다. 고맙습니다! –

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