2011-07-05 75 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字段(例如)。幫助與行動郵件

我需要什麼?我需要在我的問題(is_answered = 0)上發送電子郵件爲NOT answered的人。所以,現在的工作是這樣的:我收到的電子郵件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   "[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:找到(例如,我有100個受訪者,並且有70個人被回答)並且發送了ME電子郵件(用戶,他補充了問題確定,(它正在工作)),70個回答者回答了(並且完全正確)和誰不回答回答。只需發送一封電子郵件!

PS - 現在,我收到30個電子郵件(誰不回答),但我認爲這是錯誤的:d

回答

0

這聽起來像你正試圖在這裏做的是找到誰沒有回答問題,所有用戶上 - 所以一些調查像

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

然後你就可以通過集合(查詢,問題)的看法,並遍歷列出您的統計

希望我做了這個明確的 - 如果不是也許我可以如果您在模型上發佈關聯,則做得更好s和您正在使用的郵件視圖...

+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