?
Solved

Individual ruby logic in email template - Rails

Posted on 2009-12-22
8
Medium Priority
?
202 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:depassion
  • 4
  • 4
8 Comments
 
LVL 10

Accepted Solution

by:
Andrew Doades earned 2000 total points
ID: 26106548
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
 

Author Comment

by:depassion
ID: 26106687
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
 
LVL 10

Expert Comment

by:Andrew Doades
ID: 26106731
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:depassion
ID: 26106800
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
 

Author Comment

by:depassion
ID: 26106838
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
 
LVL 10

Expert Comment

by:Andrew Doades
ID: 26106921
Ah cool!

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

Andrew
0
 

Author Comment

by:depassion
ID: 26106986
Yeah thanks,

are you available for hire? maybe for some future projects?
0
 
LVL 10

Expert Comment

by:Andrew Doades
ID: 26107063
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question