1

I'm trying to run a background mailer and depending on the params of the article, dump different users into the mailing list. I'm getting this error upon request to make a new article:

Actor crashed!
NoMethodError: undefined method `email' for #<User::ActiveRecord_Relation:0x007fca99f657c8>

Here is the logic:

def create
    @article = Article.new(article_params)

  @all_users = []

  if @article.football == true 
    @all_users << User.where( :sport => "Football").all 

  elsif @article.basketball == true    
    @all_users << User.where("users.sport LIKE ?", "%Basketball%").all 

  elsif @article.volleyball == true 
    @all_users << User.where( :sport => "Volleyball").all 

  elsif @article.lacrosse == true 
    @all_users << User.where( :sport => "Lacrosse").all 

  else 
    @all_users = User.all
  end

    if @article.save


    @all_users.each do |user|
      ArticleMailer.new.async.article_confirmation(user,@article)

   end 
   redirect_to @article

    else
     render 'new'
    end
end
jpn
  • 285
  • 2
  • 15

1 Answers1

0

it's a bit late to answer but this issue has no answers.

Your implementation of @all_users is an array of ActiveRecord_Relation objects except in your last else statement where you use correct assignation with =.

While you are iterating on @all_users, you actually are sending User collections to the job instead of single User records.

In your case: simply use the assignation operator = in all cases instead of the append operator <<. Keep your implementation of each, which will then iterate on records.