Individual ruby logic in email template - Rails

Hi all, at the moment i am sending the same 6 random numbers to all my subscribers, i need to send every subscriber different random numbers.

the relevant code is here http://pastie.org/753321
depassionAsked:
Who is Participating?
 
Andrew DoadesConnect With a Mentor IT TechnicianCommented:
Perhaps this is what you're looking for

This will send the email for each user found, so if you have 5 users it will send 5 separate emails each with different numbers
#subscriptions controller

def deliver
    @subscription = Subscription.find(params[:id])
    @users = @subscription.users
    @recipients = @users.find(:all).collect { |user| user.email }  
    @subject = @subscription.subject
    for user in @users
    @lotto_numbers = (1..49).to_a.sort{rand() - 0.5 } [0..5]
    @subscription.deliver(@recipients, @subscription, user, @subject, @lotto_numbers)
    end
    flash[:notice] = "Delivered Subscription"
    redirect_to subscriptions_url
  end

Open in new window

0
 
depassionAuthor Commented:
thanks doades,

its close but now it sends every user every email, i previously had @recipients get all email addresses from the subscribers, now i need it to get every email individually.
0
 
Andrew DoadesIT TechnicianCommented:
I think I've maybe gone brain dead..

I don't fully understand what you mean there?

Do you want each user to get a different set of numbers, or each subscriber (subscribers@domain.com)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
depassionAuthor Commented:
sorry, i was using user and subscriber to mean the same thing. I have a list of users (2) they sign up for a subscription.

i want each user to get get an email with different random numbers, what you said worked in that now there are 2 emails being sent, but each user gets both emails.

i need to change this line to get a single email address:
@recipients = @users.find(:all).collect { |user| user.email }  

i thought that this would work, but it doesn't:
 def deliver
    @subscription = Subscription.find(params[:id])
    @users = @subscription.users
    @subject = @subscription.subject
    for user in @users
      @lotto_numbers = (1..49).to_a.sort{rand() - 0.5 } [0..5]
      @subscription.deliver(@recipients, @subscription, @user, @subject, @lotto_numbers)
      @recipients = user.email
    end
    flash[:notice] = "Delivered Subscription"
    redirect_to subscriptions_url
  end
0
 
depassionAuthor Commented:
i got it, just had to reorder:

 def deliver
    @subscription = Subscription.find(params[:id])
    @users = @subscription.users
    @subject = @subscription.subject
    for user in @users
      @lotto_numbers = (1..49).to_a.sort{rand() - 0.5 } [0..5]
      @recipients = user.email
      @subscription.deliver(@recipients, @subscription, @user, @subject, @lotto_numbers)
    end
    flash[:notice] = "Delivered Subscription"
    redirect_to subscriptions_url
  end
0
 
Andrew DoadesIT TechnicianCommented:
Ah cool!

I see now, because we had set the recipients twice. :D

Andrew
0
 
depassionAuthor Commented:
Yeah thanks,

are you available for hire? maybe for some future projects?
0
 
Andrew DoadesIT TechnicianCommented:
Sure..
I have a full-time job, but if you ever need help or work doing, I'll be more than happy to take a look and do my best!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.