我怎样才能获得.any的价值?什么时候真的? - 铁路

时间:2016-05-04 17:25:13

标签: ruby-on-rails ruby-on-rails-4

@comment.body.scan(/@(\w+)/).flatten.to_a.each do|username|
   if @mentioned = User.where(:username => username).any?
    #@mentioned value??
   end
end

如何在真正的

时获得@ mentioned的值

1 个答案:

答案 0 :(得分:2)

您想通过用户名获取User并检查是否有用户名吗?

@comment.body.scan(/@(\w+)/).flatten.to_a.each do|username|
   @mentioned = User.find_by_username(username)

   if !@mentioned.blank?
    # @mentioned is a User model instance.
    @mentioned.username  == username             # => true
   end
end