title: RANSACK GEM - Searching with Associations - Rails 4
body: <p>Any help and advise would be much appreciated.</p>
  
  <ul>
  <li>i have a company model that has many recruiters (users) &amp;</li>
  <li>a recruiter (user) belonging to a company</li>
  <li>also recruiters belong to a gender &amp; gender has many recruiters</li>
  </ul>
  
  <blockquote>
    <p>using the GEM ransack, i am trying to display all male recruiters(users) in a
    company but i am unsure of the code needed for the 'recruiters' action in the companies controller - anyway help &amp; advise would be much appreciated. Below are my files:</p>
  </blockquote>
  
  <p>Models:</p>
  
  <pre><code>user.rb
  class User &lt; ActiveRecord::Base
  belongs_to :company
  belongs_to :category_gender
  end
  
  company.rb
  class Company &lt; ActiveRecord::Base
  has_many :users
  end
  
  category_gender
  class CategoryGender &lt; ActiveRecord::Base
  has_many :users
  end
  </code></pre>
  
  <p>Companies_controller.rb</p>
  
  <pre><code>class CompaniesController &lt; ApplicationController
    respond_to :html, :xml, :json
    before_action :set_company, only: [:show, :edit, :update, :destroy]
    # load_and_authorize_resource
  
  ........
  
    def recruiters
      @search = Company.search(params[:q])
      @companies = @search.result.includes(:users)
  
      @user = current_user
      @company = @user.company
    end
  end
  </code></pre>
  
  <p>routes.rb</p>
  
  <pre><code>Rails.application.routes.draw do
    resources :companies do
      resources :comments, only: [:create, :destroy] 
      member do
        get 'recruiters'
      end
    end
  
    devise_for :users
    resources :users do
      resources :comments, only: [:create, :destroy] 
      resources :adverts 
      resources :blogs
      member do
        get 'applications'
        get 'applicants'
        get 'settings'
        get 'dashboard'
        get 'management'
      end
  end
  </code></pre>
  
  <p>recruiters.html.erb (views file):</p>
  
  <pre><code>  &lt;%= search_form_for @search, url: recruiters_company_path do |f| %&gt;
      &lt;div&gt;
        &lt;h3&gt;gender&lt;/i&gt;&lt;/h3&gt;
        &lt;ul&gt;
          &lt;% CategoryGender.all.each do |gender| %&gt;
            &lt;li&gt;
            &lt;%= check_box_tag('q[category_gender_name_eq_any][]', gender.name) %&gt;
            &lt;%= gender.name %&gt; (&lt;%= gender.users.count %&gt;)
            &lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
      &lt;button&gt;&lt;%= f.submit 'search' %&gt;&lt;/button&gt;
    &lt;% end %&gt;
  </code></pre>