Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom search with ransacker

I'm trying to add a custom filter to ActiveAdmin which is powered by Ransack these days. Unfortunately, ransacker is not documented at all and from the few resources online I fumbled together the following (in the User model):

ransacker :full_text, formatter: ->(search) {
  ids = User.search_in_all_translated(search).map(&:id)
  ids = ids.any? ? ids : nil
} do |parent|
  parent.table[:id]
end

The search_in_all_translated method returns an array of users which match the search string across all translated attributes.

On the admin page, the following filter is defined:

filter :full_text_in,
  label: 'full text search',
  as: :string

The filter itself works, so filtering tom will list all matching records. However, the value in the filter input switches to ["tom"].

Before applying the filter:

enter image description here

After applying the filter:

enter image description here

Any ideas how to fix this?

like image 900
svoop Avatar asked Mar 18 '14 07:03

svoop


People also ask

What is ransack?

Ransack is a rewrite of MetaSearch created by Ernie Miller and developed/maintained for years by Jon Atack and Ryan Bigg with the help of a great group of contributors. Ransack's logo is designed by Anıl Kılıç.

How do I use ransack with rails?

Initial commit. Ransack will help you easily add searching to your Rails application, without any additional dependencies. There are advanced searching solutions around, like ElasticSearch or Algolia. Ransack will do the job for many Rails websites, without the need to run additional infrastructure or work in a different language.

Can I use ransack with RubyGems?

Use advanced features like i18n and extensive configuration options. Ransack is supported for Rails 7.0, 6.x on Ruby 2.6.6 and later. If you would like to use the latest updates not yet published to RubyGems, use the main branch: There is extensive documentation on Ransack, which is a Docusaurus project and run as a GitHub Pages site.

What is the best search engine for a Rails website?

There are advanced searching solutions around, like ElasticSearch or Algolia. Ransack will do the job for many Rails websites, without the need to run additional infrastructure or work in a different language. With Ransack you do it all with standard Ruby and ERB.


1 Answers

There's a feature for ransackable scopes waiting to be merged: https://github.com/activerecord-hackery/ransack/pull/288

UPDATE:

I've given the work of avit and glebm another go with PR https://github.com/activerecord-hackery/ransack/pull/390 which has been merged, it's therefore now possible to use scopes with Ransack. For the documentation, see the commit:

https://github.com/svoop/ransack/commit/72dd5d12d58919bf37199234cf13f9533f3b8cd5

Here's a real-life example:

class Project < ActiveRecord::Base
  scope :full_text_search, ->(search) { search_in_all_translated(search) }

  def self.ransackable_scopes(auth_object = nil)
    [:full_text_search]
  end
end

In this example search_in_all_translated returns some complex indexed full text search SQL.

like image 142
svoop Avatar answered Sep 25 '22 15:09

svoop