Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails_admin searchable association

Tags:

rails-admin

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...

like image 642
sebastian Avatar asked Nov 23 '12 12:11

sebastian


1 Answers

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.

like image 153
Vic Avatar answered Sep 27 '22 20:09

Vic