Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add acts-as-taggable-on and polymorphic searches to documentation #1306

Merged
merged 2 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/docs/going-further/acts-as-taggable-on.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Acts-as-taggable-on
sidebar_position: 13
---

If you have an `ActiveRecord` model and you're using [acts-as-taggable-on](https://github.com/mbleigh/acts-as-taggable-on),
chances are you might want to search on tagged fields.

Suppose you have this model:

```rb
class Task < ApplicationRecord
acts_as_taggable_on :projects
end
```

and you have the following two instances of `Task`:

```rb
{ id: 1, name: 'Clean up my room', projects: [ 'Home', 'Personal' ] }
{ id: 2, name: 'Complete math exercises', projects: [ 'Homework', 'Study' ] }
```

When you're writing a `Ransack` search form, you can choose any of the following options:

```erb
<%= search_form_for @search do |f| %>
<%= f.text_field :projects_name_in %> <!-- option a -->
<%= f.text_field :projects_name_eq %> <!-- option b -->
<%= f.text_field :projects_name_cont %> <!-- option c -->
<% end %>
```

### Option a - match keys exactly

Option `a` will match keys exactly. This is the solution to choose if you want to distinguish 'Home' from 'Homework': searching for 'Home' will return just the `Task` with id 1. It also allows searching for more than one tag at once (comma separated):
- `Home, Personal` will return task 1
- `Home, Homework` will return task 1 and 2

### Option b - match key combinations

Option `b` will match all keys exactly. This is the solution if you wanna search for specific combinations of tags:
- `Home` will return nothing, as there is no Task with just the `Home` tag
- `Home, Personal` will return task 1

### Option c - match substrings

Option `c` is used to match substrings. This is useful when you don't care for the exact tag, but only for part of it:
- `Home` will return task 1 and 2 (`/Home/` matches both `"Home"` and `"Homework"`)
40 changes: 40 additions & 0 deletions docs/docs/going-further/polymorphic-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Polymorphic Searches
sidebar_position: 14
---

When making searches from polymorphic models it is necessary to specify the type of model you are searching.

For example:

Given two models

```ruby
class House < ActiveRecord::Base
has_one :location, as: :locatable
end

class Location < ActiveRecord::Base
belongs_to :locatable, polymorphic: true
end
```

Normally (without polymorphic relationship) you would be able to search as per below:

```ruby
Location.ransack(locatable_number_eq: 100).result
```

However when this is searched you will get the following error

```ruby
ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :locatable
```

In order to search for locations by house number when the relationship is polymorphic you have to specify the type of records you will be searching and construct your search as below:

```ruby
Location.ransack(locatable_of_House_type_number_eq: 100).result
```

note the `_of_House_type_` added to the search key. This allows Ransack to correctly specify the table names in SQL join queries.