← Back to Upcase

Sending last 5 emails to new subscribers


(charlieanna) #1

Suppose there is a new post and as soon the post is created I want to send this post as a link in an email to all the subscribers and this is ok. But what if there is a new subscriber and I wish to send him the last sent email then how do I do this?

I am guessing as soon as the new subscription is created I should retrieve the last email and send it to him. I dont think emails are stored so I should recreate a new email with a link to last post.

class Subscription 
   def create
     @user = User.find(params[:id])
     add_user_to_mailing_list(@user)   
     send_last_email_to_user(@user)
   end

  def send_last_email_to_user(user)
    last_post = Post.last    #what if this was a complex query like getting the last 5 posts? Should this go inside the Post model? 
    email = create_an_email(post)
    send_email(email)  
 # where should the previous two methods go inside? 
  end
end

Or instead of sending an email from here, there be a watcher or an observer who listens for new subscriptions so to keep this logic away from here?

Whenever there is a new post I can add a watcher which listens for any changes to the Post model and use an after_create callback to send the emails.

But should there be a new watcher listening for new subscriptions?