admin searchable association

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


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

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.

链接地址: http://www.djcxy.com/p/11492.html

上一篇: 用于JDK 1.5的Collections.newSetFromMap的替代方法?

下一篇: 管理员可搜索的关联