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
I have table inquiry
(relationship table) with is_answered
field (for example).
What I need? I need to send email people who are NOT answered
on my question (is_answered = 0
). So Now works this way: i received 2
emails (because i have in database 1 question and 2 users who are not answered) :
id | question_id | is_answered
14 | 11 | 0
24 | 11 | 0
So, i need to receive ONLY ONE EMAIL
not two!!! In email i want to write some statistics about question. But i need only ONE EMAIL! how can i possible do it ?
thank you!
------------------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: find all inquries where is_answered = 0 (For example i have 100 respondents, and 70 people are answered) and send ME email (user, who added questio开发者_StackOverflown ok, (it is working)) that 70 respondents answered (AND WHO EXACTLY) and who not answered. JUST SEND ONE EMAIL!
PS - Now i received 30 emails (who are not answered), but i think it is wrong :D
It sounds like what you are trying to do here is to find all users who have not answered questions on an inquiry -so something like
`User.all(:includes=>[:questions, :inquiries], :conditions=>["is_answered=?",0])
Then you can pass the collections (inquiries,questions) to the view and iterate through to list your statistics
Hope I made this clear - if not perhaps I can do better if you post the associations on the models and the view you are using for the mailer ...
OK, I think see what you are doing now! How about
#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
精彩评论