我将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,它包含与名称(first_name
和last_name
)以及locale
和name_id
相同的属性。< / p>
在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_name
中寻找名为people
的列,而不是加入/包括names
和name_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中完成吗?谢谢你的帮助...
答案 0 :(得分:2)
我遇到了与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
答案 1 :(得分:1)
从this thread开始,我遵循了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}]
似乎可以使其正常工作。