2011-07-05 6 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 answeredis_answered = 0)の電子メールを送る必要があります。だから、今、このように動作します:(iのデータベースに1つの質問と回答されていない2人のユーザーを持っているので)私は2メールを受け取った:

だから、
id | question_id | is_answered 
14 |  11  |  0 
24 |  11  |  0 

、私はONLY ONE EMAILない2を受信する必要があります!電子メールで私は質問に関するいくつかの統計を書いてみたいです。しかし、私は1つの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人(正確には誰でも)と、回答者70人が回答したME電子メール(私は100人の回答者と70人の回答があります)答えた。必ず1つのEメールを送信してください!

PS - 今私は(答えていません)30通の電子メールを受け取ったが、私はそれが間違っていると思う:あなたがここでやろうとしているが質問に答えていないすべてのユーザーを見つけることであるようにD

答えて

0

それは聞こえます

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

のようなもの-SO問い合わせに続いて、あなたはビューにコレクション(お問い合わせ、ご質問)を渡すことができますし、あなたの統計私は、これは明らかにされ

希望リストするために反復処理 - おそらく私ができない場合をモデルに関連付けを投稿するとより効果的ですあなたとメーラーのために使用しているビュー...

+0

助けてくれてありがとう。私は自分の投稿を更新しました。ありがとうございました! –

0

[OK]を、あなたは今何をしていると思います!方法について

#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