管理员可搜索的关联

我将rails_admin和globalize3一起使用,无法获得可搜索的关联。 这里是模型(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

NameTranslation模型来自globalize3,它包含与name( first_namelast_name )以及localename_id相同的属性。

config/initializers/rails_admin.rb我有

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

然后,在GUI中,当我在name上添加过滤器时,我得到:

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

显然,rails_admin正在寻找一个名为列name_translations.last_namepeople ,而不是加盟/包括namesname_translations -为什么?

我需要的rails_admin是这样的,在irb中工作:

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

它会生成以下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")

这可以在rails_admin中完成吗? 谢谢你的帮助...


has one关系有类似的问题。 我解决这个问题的方式是在模型上设置一个default_scope并将其与关联的表连接起来(这是唯一能让rails admin加入这两个表的方法)。

我还必须在关联的字段上设置queryable true

想象一下,你只需要在名称关联中搜索,然后以下是它的工作方式:

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

但是,您需要搜索has many关联,并且我不知道这种方法是否仍然有效,但这里有个猜测:

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

从这篇文章中,我跟随了Nick Roosevelt的建议,并且为我的案子工作

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

我试过searchable room: :name ,但它不起作用,但searchable [{Room => :name}]似乎可以使其工作。

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

上一篇: admin searchable association

下一篇: Easy way to Sync Data between MongoDB and Apache Solr