John
John

Reputation: 1323

What is the difference between deliver & deliver_now in rails?

There is not clear explanation between deliver & deliver_now in rails mailers.

if we use deliver_now it won't queue up for an active job. if we use deliver_later it will be executed by active_job (background job).

when deliver & deliver_now are not executed in the background, then what exactly the difference between them? I searched a lot but did not get a clear idea. between deliver and deliver_now.

is there any difference or both are the same

Upvotes: 2

Views: 2188

Answers (2)

Buck3000
Buck3000

Reputation: 371

The short answer is that #deliver_now is a wrapper for #deliver and uses #deliver under the hood in the Rails source code. There's no major difference. Both will work - even in Rails 6+ - but #deliver_now is preferred because it's more overt about what the code is doing / what is happening.

In practice, you almost always want to use #deliver_later and to use a background server like Sidekiq with Redis, GoodJob (or SolidQueue in Rails 8) - if I'm not mistaken #deliver_now can be blocking for a web request - so best to offload that to a background process to speed up your Rails application's response time for your users.

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230326

They're the same. Use deliver_now, not deliver.

https://apidock.com/rails/v4.2.9/ActionMailer/MessageDelivery/deliver

    def deliver #:nodoc:
      ActiveSupport::Deprecation.warn(        `#deliver` is deprecated and will be removed in Rails 5. Use        `#deliver_now` to deliver immediately or `#deliver_later` to        deliver through Active Job..squish)

      deliver_now
    end

Upvotes: 6

Related Questions