문제

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