views:

34

answers:

1

I have a rake task that sends out the next 'x' invitations to join a beta it uses this code:

desc "This will send out the next batch of invites for the beta"
task :send_invites => :environment do
  limit = ENV['limit']
  c = 0
  invitation = Invitation.all(:conditions => { :sent_at => nil, :sender_id => nil }, :limit => limit).each do |i|
    Mailer.deliver_invitation(i, register_url(i.token))
    c.increment!
  end

  puts "Sent #{c} invitations."
end

I need to pass in the 'register_url' to the Mailer in order for the link to show up in the email, but since this is running from a rake task and not from a request it does not have a access to the helper methods. What is the best way of achieving this?

A: 

Are you using an ActionMailer to deliver the messages? It seems like it. If so, you can just pass off the token and have the Mailer itself format the token using the register_url. So you would do:

invitation = Invitation.all(:conditions => { :sent_at => nil, :sender_id => nil }, :limit => limit).each do |i|
    Mailer.deliver_invitation(i)
    c.increment!
end

and then in your invite template you would just use

<%= register_url(i.token) %>
christophercotton