Save 36% for Black Friday! Learn more

Paul Bernhard


120 Experience
1 Lesson Completed
0 Questions Solved


Posted in How to use Action Mailbox in Rails 6 Discussion

How come the mail and inbound_mail objects are available to all instance methods?

This question is not only related to ActionMailbox, but i will still post it here as i never saw a similar use of "local variables" before your episode on ActionMailbox, altough i am not sure if those are actually "local variables".

In your replies_mailbox.rb you have the following code:

class RepliesMailbox < ApplicationMailbox
  def process
    return if user.nil?

  def user
    @user =|| User.find_by(email: mail.from)
        # the mail object is available here

# …

Somehow the mail and the inbound_mail objects seem to be available to all instance methods of RepliesMailbox. This is an interesting functionality because you don't have to define all methods like def user(mail), def discussion(mail), def discussion_id(mail), if they all refer to the same mail object anyway.

I am not sure if the mail and inbound_mail are actually "local variables" and maybe i am missing out on a fundamental knowledge of variable scope. But how come you can access those objects throughout all methods? Can this way of "reusing" variables be used in all Ruby / Rails code?

Hi, thanks for this helpful introduction on ActiveStorage.
Does ActiveStorage provide a solution to process uploads on upload and not on-the-fly? For ex. processing video-files could be time-consuming and good to be done on upload.
If there is no option to process on upload, is there a reason to prefer on-the-fly processing?
logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.