MKK
MKK

Reputation: 2753

How can I avoid array or single error?

When @notification_users contains more than 2 arrays, this won't be error.
However, it returns this error undefined method+' for nil:NilClasswhen@notification_users.count.to_s` = 1

How can I avoid error even when it has only one record(Not array)?

@notification_users.each do |user|
    @users_emails += [user.email]
end

Upvotes: 0

Views: 37

Answers (1)

Huy
Huy

Reputation: 11206

The error is actually stating that @users_emails is nil when you try to append/push an element into that object.

You can initialize @users_emails as an empty array and then push elements into your array.

@users_emails = []
@notification_users.each do |user|
  @users_emails << user.email
end

This ensures that @users_emails will always be an array.

Upvotes: 2

Related Questions