Returning an array of objects in rails

I need to pass an array of mailboxes from my rails mailer class to the appropriate controller, which I thought should work if I just do

class foo < Actionmailer::Base

    def bar(...)
        mails_array = Array.new
        return mails_array
    end

but since the controller gets mails_arraythrough

@mails = Array.new
@mails.concat(foo.bar(...))

I get a:

TypeError in mailsController # index
cannot convert Mail :: Message to Array

Did I miss something? I would expect mails_array to be in the letters and don’t understand why this is not so.

+3
source share
2 answers

You call foo.bar, but it baris defined as an instance method, not a class method. Try

class foo < Actionmailer::Base      

    def self.bar(...)
        mails_array = Array.new
        return mails_array
    end

instead.

+2
source

Array#concat , , Mail::Message. :

@mails << foo.bar(...)

@mails.push(foo.bar(...))
0

Source: https://habr.com/ru/post/1779664/


All Articles