Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:13:19+00:00 2026-06-03T20:13:19+00:00

Using this railscast http://railscasts.com/episodes/127-rake-in-background?autoplay=true as an example/inspiration (i.e. I’m not trying to implement the

  • 0

Using this railscast http://railscasts.com/episodes/127-rake-in-background?autoplay=true as an example/inspiration (i.e. I’m not trying to implement the rails cast code, just use it as an inspiration), I tried to move a mailer, that was triggered by an after_create callback in the user.rb model, into a rake task, so it would run in the background. The mailer was working before I moved it into a rake task, but it’s not working anymore.

Instead of calling the mailer from the User.rb model, which it was how it was set up originally (see the commented out code in user.rb), I instead call the rake task, which then invokes the UserMailer.welcome_email method.

In the original code, “self” was submitted (from User.rb) as a parameter to the method welcome_email(user) in user_mailer.rb. In my attempt to turn it into a rake task, I assigned “self” to USER_INSTANCE, which is supposed to be picked up in the mailer.rake as ENV[“USER_INSTANCE”]. This was also suggested by the railscast.

Somewhere along the way it’s not working. Any ideas?

User.rb

 after_create :send_welcome_email



  def send_welcome_email
    system "rake :send_mailing USER_INSTANCE=self &"   #note the & forks the task
    #UserMailer.welcome_email(self).deliver  <-- how it was originally. 
  end

mailer.rake

desc "Send mailing"
task :send_mailing => :environment do

 UserMailer.welcome_email(ENV["USER_INSTANCE"]).deliver  #moved from user.rb to here but now with environment variable instead of parameter
end

unchanged User_mailer.rb

 class UserMailer < ActionMailer::Base
  default :from => "blahblah@gmail.com"

  def welcome_email(user)
    mail(:to => user.email, :subject => "Invitation Request Received")
  end

end

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:13:21+00:00Added an answer on June 3, 2026 at 8:13 pm

    currently you are doing this

    system "rake :send_mailing USER_INSTANCE=self &"

    which is the same as going to the command line and typing

    rake :send_mailing USER_INSTANCE=self &

    self is just a literal string, I think what you are trying to do is this

    system "rake :send_mailing USER_INSTANCE=#{self} &"

    but that will end up being the equivalent of running this on the command line

    rake :send_mailing USER_INSTANCE=<User::xxxxx>

    rake won’t serialize this into your User ActiveRecord object;
    when you shell out with system there is no relation to the calling code

    an alternative – your rake task could take an integer – user_id and then access the record via User.find
    but it gets more complicated as after_create is going to be running in a transaction so once your rake task runs it may or may not have finished that transaction

    I would advise against trying to re-invent a way to do background processing in rails, there are already good tried and true solutions available

    see http://railscasts.com/?tag_id=32 for some options

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Rails 3.0.7 and I am trying to follow this Railscast: http://railscasts.com/episodes/182-cropping-images
I have a multi-step form (a wizard) that I created using this railscast: http://railscasts.com/episodes/217-multistep-forms
using this tutorial http://railscasts.com/episodes/57-create-model-through-text-field need to make it work in my app, was on
I've been following Ryan Bates Railscast on Subdomains http://railscasts.com/episodes/221-subdomains-in-rails-3 and using lvh.me to serve
It is easy, following this railscast http://railscasts.com/episodes/240-search-sort-paginate-with-ajax to be able to create a table
In this movie http://media.railscasts.com/videos/181_include_vs_joins.mov you can see he is using a custom ./script/console and
I am creating request specs following the Railscast at http://railscasts.com/episodes/257-request-specs-and-capybara In my application, users
I have seen the railscast on self-referential relationships here: http://railscasts.com/episodes/163-self-referential-association I have built upon
Trying to implement Ryan's JS implementation of the facebook login, via his Railscast: http://railscasts.com/episodes/360-facebook-authentication
I'm following this rails cast to create a nested form: http://railscasts.com/episodes/196-nested-model-form-part-1 I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.