def licences_query(users) return users unless params[:licence_ids] users_scope = users licence_ids = params[:licence_ids] if params[:licence_ids].is_a? String licence_ids = params[:licence_ids].split(',').map{|chr| chr.to_i} end # 4 - Door Supervision # 2 - CCTV # Return all users with Door Supervision if there is no CCTV selected # Otherwise, require exact match (else) if licence_ids.include?(4) && !licence_ids.include?(2) users_scope = users_scope.joins("INNER JOIN guard_licences as #{rel} ON users.id=#{rel}.user_id") .where("#{rel}.licence_id=4") .where("#{rel}.status=?", 'active') else licence_ids.each_with_index do |licence_id, index| rel = "gl#{index}" users_scope = users_scope.joins("INNER JOIN guard_licences as #{rel} ON users.id=#{rel}.user_id") .where("#{rel}.licence_id=?", licence_id) .where("#{rel}.status=?", 'active') end end users_scope end