Problem: Get a single record from the contacts table where the first_name and last_name is equal to those given in the params. If there is more than one record found, then return the record that matches a domain.

def check_cache(params)
  cached = where(first_name: params[:first_name], last_name: params[:last_name])
  if cached.size > 1
    # select the record with a matching one of params[:domains]
    # cached #=> ['bob@gmail.com', 'bob@yahoo.com']
    # params[:domains] #=> ['gmail.com', 'abc.com']
    # result would be bob@gmail.com
  end
  cached
end

tried this in IRB

cached.select{|e| e =~ /(gmail.com)/}

but not sure how I would check each one in the params[:domains]

有帮助吗?

解决方案

try this:

if cached.size > 1   
  params[:domains].each do |domain|
    cached.select do |result|
      result_domain = result.split("@").last

      return result if result_domain == domain
    end
  end
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top