ruby - Remove element from array unless has a certain value in Rails -
i have array of "names" , display current user's name if there name in array. if there other names in array, don't want show current user's name.
currently, listing names , excluding current user's name, don't want exclude current user's name if 1 in array. hope explained okay.
my code now:
module conversationshelper def other_user_names(conversation) users = [] conversation.messages.map(&:receipts).each |receipts| users << receipts.select |receipt| receipt.receiver != current_user end.map(&:receiver) end users.flatten.uniq.map(&:name).join ', ' end end
this should work:
def other_user_names(conversation) # users (no duplicates) users = conversation.messages.map(&:receipts).map(&:receiver).uniq # remove current user if there more 1 users users.delete(current_user) if users.many? # return names users.map(&:name).join(', ') end
i move first line conversation#users
method.
Comments
Post a Comment