rails_admin searchable association
Asked Answered
D

2

8

I am using rails_admin together with globalize3 and cannot get searchable associations to work. Here are the models (Person has_one/belongs_to Name has_many/belongs_to NameTranslation):

class Person < ActiveRecord::Base
  has_one :name, inverse_of: :person
end

class Name < ActiveRecord::Base
  belongs_to :person, inverse_of: :name
  translates :first_name, :last_name
  has_many :name_translations, inverse_of: :name, dependent: :destroy
end

class NameTranslation < ActiveRecord::Base
  belongs_to :name, inverse_of: :name_translations      
end

The NameTranslation model is coming from globalize3, it contains the same attributes as name (first_name and last_name) plus locale and name_id,.

In config/initializers/rails_admin.rb I have

config.model Person do
  list do
    field :name do
      searchable name_translations: :last_name
    end
  end
end

Then, in the GUI, when I add a filter on name, I get:

SQLite3::SQLException: no such column: name_translations.last_name: SELECT  "people".* FROM "people"  WHERE (((name_translations.last_name LIKE '%freud%'))) ORDER BY people.id desc LIMIT 20 OFFSET 0

Obviously, rails_admin is looking for a column named name_translations.last_name in people instead of joining/including names and name_translations - why?

What I need rails_admin to do is this, working in irb:

>> Person.joins( name: :name_translations ).where('name_translations.last_name like "test"')

which generates the following SQL:

SELECT "people".* FROM "people" INNER JOIN "names" ON "names"."person_id" = "people"."id" INNER JOIN "name_translations" ON "name_translations"."name_id" = "names"."id" WHERE (name_translations.last_name like "test")

Can this be done in rails_admin? Thanks for your help...

Degrease answered 23/11, 2012 at 12:56 Comment(2)
Did you ever get this figured out? I know it's been a while.Mcafee
No, I didn't, sorry...Degrease
G
6

From this thread, I followed Nick Roosevelt's suggestion and it worked for my case

class Room < ActiveRecord:Base
  has_many :time_slots
end

class TimeSlot < ActiveRecord::Base
  belongs_to :room

  rails_admin do
    list do
      field :day do
        searchable true
      end
      # field :room do
      #   searchable room: :name
      # end
      field :room do
        searchable [{Room => :name}]
        queryable true
      end
    end
  end
end

I tried searchable room: :name and it was not working, but searchable [{Room => :name}] seem to make it work.

Guaco answered 9/5, 2016 at 10:12 Comment(0)
C
4

I had a similar problem with a has one relationship. The way I solved it was to set a default_scope on the model and join it with the associated table (it is was the only way I could get rails admin to join these two tables).

I also had to set queryable true on the associated field.

Imagine that you had to search only inside the name association, then here's how it would work:

class Person < ActiveRecord::Base
  has_one :name, inverse_of: :person

  default_scope { eager_load(:name) }
end

config.model Person do
  list do
    field :name do
      queryable true
      searchable [:column1, :column2, ..]
    end
  end
end

However, you need to search through the has many association and I don't know whether that approach would still work, but here's a guess:

class Person < ActiveRecord::Base
  has_one :name, inverse_of: :person
  has_many :name_translations, through: :name

  default_scope { eager_load(:name_translations) }
end

config.model Person do
  list do
    field :name_translations do
      queryable true
      searchable :last_name
    end
  end
end
Chianti answered 3/4, 2017 at 12:8 Comment(1)
I can verify that this works on Rails 5, Rails_admin v1.1. The association appears in the list of fields in the filter, but until the queryable - searchable lines are put in, it just shows the entire list unfiltered. There is no error or any clue what is wrong. This makes the filter/search work as expected. You can see a full working example here.. github.com/dgleba/hrapp361Autotoxin

© 2022 - 2024 — McMap. All rights reserved.