ARTLoe
ARTLoe

Reputation: 1799

RANSACK GEM - Searching with Associations - Rails 4

Any help and advise would be much appreciated.

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 & advise would be much appreciated. Below are my files:

Models:

user.rb
class User < ActiveRecord::Base
belongs_to :company
belongs_to :category_gender
end

company.rb
class Company < ActiveRecord::Base
has_many :users
end

category_gender
class CategoryGender < ActiveRecord::Base
has_many :users
end

Companies_controller.rb

class CompaniesController < 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

routes.rb

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

recruiters.html.erb (views file):

  <%= search_form_for @search, url: recruiters_company_path do |f| %>
    <div>
      <h3>gender</i></h3>
      <ul>
        <% CategoryGender.all.each do |gender| %>
          <li>
          <%= check_box_tag('q[category_gender_name_eq_any][]', gender.name) %>
          <%= gender.name %> (<%= gender.users.count %>)
          </li>
        <% end %>
      </ul>
    </div>
    <button><%= f.submit 'search' %></button>
  <% end %>

Upvotes: 0

Views: 1133

Answers (1)

Jay-Ar Polidario
Jay-Ar Polidario

Reputation: 6603

Since you are searching for Users. Then, .ransack method should be called on User model and not on Company model.

Try the following in your recruiters method:

def recruiters
  @company = Company.find(params[:id])
  @users = User.ransack(params[:q]).result.where(company: @company)

  @user = current_user
end
  • 1st line: I used @company = Company.find(params[:id]) instead of @company = @user.company because you are using resources pathing for Company, in which recruiter method is a "member" of (as you defined in your routes.rb).

    The problem with @user.company is that say for example a user browses /companies/1/recruiters, the @company that will be assigned is going to be the same as when browsing /companies/3/recruiters. But by convention, @company in these two paths should already be different because of the id changing.

  • 2nd line: I just added a the predicate .where(company: @company) so that the results will be filtered to the respective company.

Then in your recruiters.html.erb view file, you could just add something like the following:

...
...
<% @users.each do |user| %>
  <%= user.name %>
<% end %>
...
...

Upvotes: 1

Related Questions