title: How can i refactor this? And also make less calls to the server?
body: <p>i have this method. Its job is to return a list of trainees who have now graduated to developers. The user filters what skills they are looking for in a developer. The list is returned with the more relevant developers on the top of the list</p>
  
  <p>this is the controller method:</p>
  
  <pre><code>  def filter
      @skills = Skill.all
      @developers = []
      unless params[:ids].nil?
        params[:ids].each do |skill|
          skill = @skills.find(skill)
          skill.trainees.each do |developer|
            @developers &lt;&lt; developer
          end
        end
      end
      if @developers.empty?
        @developers = Trainee.developers.all
      else
        @developers = @developers.group_by {|x| x}.map {|k, v| [k, v.count]}
        @developers.sort_by!(&amp;:last).reverse!
        @developers.map! do |developer|
          developer[0]
        end
      end
      respond_to do |format|
        format.js {}
      end
    end
  </code></pre>
  
  <p>Currently, this same method is hitting the server more times that i would like. the server log below is showing as a result of me clicking 11 buttons to filter with. can be 1 or 5 or 20 depending on what the user is looking for</p>
  
  <pre><code>Processing by SkillsController#filter as */*
    Parameters: {"ids"=&gt;["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]}
    Skill Load (0.6ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 1 LIMIT 1
    Trainee Load (0.6ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 1
    Skill Load (0.4ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 2 LIMIT 1
    Trainee Load (0.6ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 2
    Skill Load (0.3ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 3 LIMIT 1
    Trainee Load (1.3ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 3
    Skill Load (6.1ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 4 LIMIT 1
    Trainee Load (0.6ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 4
    Skill Load (0.3ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 5 LIMIT 1
    Trainee Load (0.9ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 5
    Skill Load (2.6ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 6 LIMIT 1
    Trainee Load (2.0ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 6
    Skill Load (4.2ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 7 LIMIT 1
    Trainee Load (0.9ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 7
    Skill Load (1.2ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 8 LIMIT 1
    Trainee Load (3.0ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 8
    Skill Load (0.3ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 9 LIMIT 1
    Trainee Load (0.8ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 9
    Skill Load (2.1ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 10 LIMIT 1
    Trainee Load (0.6ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 10
    Skill Load (0.3ms)  SELECT  `skills`.* FROM `skills` WHERE `skills`.`id` = 11 LIMIT 1
    Trainee Load (0.8ms)  SELECT `trainees`.* FROM `trainees` INNER JOIN `mastered_skills` ON `trainees`.`id` = `mastered_skills`.`trainee_id` WHERE `mastered_skills`.`skill_id` = 11
    Rendering skills/filter.js.haml
    Skill Load (0.7ms)  SELECT `skills`.* FROM `skills` INNER JOIN `mastered_skills` ON `skills`.`id` = `mastered_skills`.`skill_id` WHERE `mastered_skills`.`trainee_id` = 5
    Skill Load (0.5ms)  SELECT `skills`.* FROM `skills` INNER JOIN `mastered_skills` ON `skills`.`id` = `mastered_skills`.`skill_id` WHERE `mastered_skills`.`trainee_id` = 2728
    Skill Load (0.6ms)  SELECT `skills`.* FROM `skills` INNER JOIN `mastered_skills` ON `skills`.`id` = `mastered_skills`.`skill_id` WHERE `mastered_skills`.`trainee_id` = 10
  </code></pre>