Rails ActionMailer with() params vs Parameters Directly on the Method

115 views Asked by At

In Rails ActionMailer, you can send parameters to the mailer either by:

with() params:

Class MyMailer
  def send_something
    @user = User.find(params[:user_id])
    ..
  end
end

MyMailer.with(user_id: user.id).send_something.deliver_now

or directly in the method call:

Class MyMailer
  def send_something(user_id)
    @user = User.find(user_id)
    ..
  end
end

MyMailer.send_something(user.id).deliver_now

Are one of these methodologies preferred? Is there any advantage or disadvantage when the mailer is enqueued by a background job such as sidekiq which serializes all parameters to redis? I did note with https://guides.rubyonrails.org/action_mailer_basics.html, they only show the with() params way to do this. Is one of these ways an older style?

1

There are 1 answers

1
mmx900 On

Using params is for sharing various things between methods. ActionMailer::Parameterized API explains that with an example.

Provides the option to parameterize mailers in order to share instance variable setup, processing, and common headers.

It is not described in the guide.