3

Can't figure out how to filter nested association belongs to while search in rails admin.

I have the following code in my rails_admin config in model.

class Painting < ApplicationRecord
...
rails_admin do
  configure :translations, :globalize_tabs

  create do
    field :author do
      searchable [:last_name]
      queryable [:last_name]
      filterable true
    end

    field :genre
    field :technique
    field :material
    field :active
    field :painting_images
    field :author_preview
    field :translations
  end
end

rails_admin_filter_search

I tried every configuration in model. But nothing works. Whatever I enter in this dropdown input is being ignored.

I have the following logs in puma server.

 Started GET "/admin/author?associated_collection=author&compact=true&current_action=create&source_abstract_model=painting&query=George" for 127.0.0.1 at 2018-03-14 22:07:11 +0300
Processing by RailsAdmin::MainController#index as JSON
  Parameters: {"associated_collection"=>"author", "compact"=>"true", "current_action"=>"create", "source_abstract_model"=>"painting", "query"=>"George", "model_name"=>"author"}
  Admin Load (0.3ms)  SELECT  "admins".* FROM "admins" WHERE "admins"."id" = $1 ORDER BY "admins"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Painting Load (0.3ms)  SELECT  "paintings".* FROM "paintings" WHERE "paintings"."id" IS NULL ORDER BY "paintings"."id" DESC LIMIT $1  [["LIMIT", 1]]
   (0.3ms)  SELECT COUNT(*) FROM "authors"
  Author Load (0.4ms)  SELECT  "authors".* FROM "authors" ORDER BY authors.id desc LIMIT $1  [["LIMIT", 30]]
  Author::Translation Load (0.6ms)  SELECT "author_translations".* FROM "author_translations" WHERE "author_translations"."author_id" IN (114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85)
Completed 200 OK in 61ms (Views: 1.4ms | ActiveRecord: 1.9ms)

The thing is everything I enter in input is being processed in parameters but never gets evaluated. So it just ignored, it doesn't filter associated collection by params[:query]

I use globalize gem for translations and don't know how to properly configure rails admin to use it filters.

I did searching in original repo. I think somehow is wrong this

def get_collection(model_config, scope, pagination)
  ...
  options = options.merge(query: params[:query]) if params[:query].present?
  ...
  model_config.abstract_model.all(options, scope)
end

This query is available here, but it's ignored there. I tried to use model_config.abstract_model.all(options, scope).where(last_name: params[:query]) And this is working. But this is definitely wrong. Can someone help me with more generic approach to this problem?

Peter T.
  • 7,127
  • 3
  • 31
  • 30
zhisme
  • 1,540
  • 1
  • 14
  • 20
  • Is the author's last_name displayed in the dropdown? If no, you need to redefine `author_name` method for RailsAdmin – Vasilisa Mar 15 '18 at 04:02
  • I found an ugly hack to resolve this. If someone will meet this problem, I can help with **not** very well solution. – zhisme Apr 03 '18 at 06:07

2 Answers2

1

I had the same problem with rails_admin and globalize gems. I fixed the issue by defining a custom search for a model. For example

config.model 'Author' do
  list do
    search_by :search_by_name
    field :id
    field :name
  end
end

and inside Author model, define search_by_name scope and it will fix both searching in Author list page and dropdown in other pages.

scope :search_by_name, -> (term) {
    if term.present?
      with_translations.where('author_translations.name like ?', FormatString.new(term, :sql_match).to_s)
    else
      where({})
    end
}

Ref: https://github.com/sferik/rails_admin/wiki/Custom-Search

Peter T.
  • 7,127
  • 3
  • 31
  • 30
0

Rails admin will search associations by using the search defined for that model. Meaning you can't define the search on the Painting model but you need to do it on the Author model.

So any search that works when listing Authors will work on that dropdown. Another thing you need to be aware is that rails admins will ask by default object.title or object.name to display that name. You can define those as methods on your Author model. If you want another method you can configure what rails admin use on config/initializers/rails_admin.rb

RailsAdmin.config do |config|
  config.label_methods = [:rails_admin_title, :name]
end
Guillermo Siliceo Trueba
  • 3,332
  • 4
  • 30
  • 46