Rails4:使用.include?和LIKE在数组查询中

时间:2016-08-13 07:13:24

标签: ruby-on-rails-4 activerecord

我有一个数组:

@available_languages = ["English", "German", "Japanese"]

和变量:

@needed_language = "Basic English"

我想展示'可用!'在视图中,如果@available_languages数组中存在@needed_language,即使名称不完全匹配,例如“英语”和“基本英语”

现在,我的条件只对完全匹配返回true。如何针对非完全匹配返回true?

- if @available_languages.map{|a| a.available_language_name}.include? @needed_language.needed_language_name
  Available!
- else
  Not Available

1 个答案:

答案 0 :(得分:0)

您可以使用select函数,如下所示:

@available_languages = ["English", "German", "Japanese"]
@needed_language = "Basic English"
if @needed_language.split(" ").select{ |a| a if @available_languages.include? a} then 
  "Available" 
else 
  "Not Available" 
endif