Sha256: 673483afbfa87b5090db6d51c8a249890a39a287535b871aa13ef425776676cf

Contents?: true

Size: 1.61 KB

Versions: 13

Compression:

Stored size: 1.61 KB

Contents

---
sidebar_position: 1
title: Associations
---

### Associations

You can easily use Ransack to search for objects in `has_many` and `belongs_to`
associations.

Given these associations...

```ruby
class Employee < ActiveRecord::Base
  belongs_to :supervisor

  # has attributes first_name:string and last_name:string
end

class Department < ActiveRecord::Base
  has_many :supervisors

  # has attribute title:string
end

class Supervisor < ActiveRecord::Base
  belongs_to :department
  has_many :employees

  # has attribute last_name:string
end
```

... and a controller...

```ruby
class SupervisorsController < ApplicationController
  def index
    @q = Supervisor.ransack(params[:q])
    @supervisors = @q.result.includes(:department, :employees)
  end
end
```

... you might set up your form like this...

```erb
<%= search_form_for @q do |f| %>
  <%= f.label :last_name_cont %>
  <%= f.search_field :last_name_cont %>

  <%= f.label :department_title_cont %>
  <%= f.search_field :department_title_cont %>

  <%= f.label :employees_first_name_or_employees_last_name_cont %>
  <%= f.search_field :employees_first_name_or_employees_last_name_cont %>

  <%= f.submit "search" %>
<% end %>
...
<%= content_tag :table do %>
  <%= content_tag :th, sort_link(@q, :last_name) %>
  <%= content_tag :th, sort_link(@q, :department_title) %>
  <%= content_tag :th, sort_link(@q, :employees_last_name) %>
<% end %>
```

If you have trouble sorting on associations, try using an SQL string with the
pluralized table (`'departments.title'`,`'employees.last_name'`) instead of the
symbolized association (`:department_title)`, `:employees_last_name`).

Version data entries

13 entries across 12 versions & 2 rubygems

Version Path
ransack-4.3.0 docs/docs/going-further/associations.md
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/ransack-4.2.1/docs/docs/going-further/associations.md
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/ransack-4.2.1/docs/docs/going-further/associations.md
ransack-4.2.1 docs/docs/going-further/associations.md
ransack-4.2.0 docs/docs/going-further/associations.md
ransack-4.1.1 docs/docs/going-further/associations.md
ransack-4.1.0 docs/docs/going-further/associations.md
ransack-4.0.0 docs/docs/going-further/associations.md
ransack-3.2.1 docs/docs/going-further/associations.md
ransack-3.2.0 docs/docs/going-further/associations.md
ransack-3.1.0 docs/docs/going-further/associations.md
ransack-3.0.1 docs/docs/going-further/associations.md
ransack-3.0.0 docs/docs/going-further/associations.md