ruby-on-railsmailer

Rails, passing an object to mailer


I've got a theoretical question about mailer in Rails. I've heard a pair of times that passing resources directly to mailer is not considerated as good practice. And that is its better simply pass an id to load the object. For example exactly this thing is said in the accepted answer in this question: Rails: Absolutely stumped with delayed_job. Not receiving arguments anywhere

But if it is so, what are the reasons of this prohibition? I can not treat linked question as an exhaustive explanation. Thank you for your suggestions.


Solution

  • Reason is simple: it's more than 0% probability that your object will be changed between the time you're ordered to send mail and real mailer code execution. Imagine that object you're using will be deleted in process but since you don't have loaded your object at the execution time it'll try to use instance of object that's not exist anymore. I guess now you understand the problem.