gpt4 book ai didi

rails-admin - Rails_admin 可搜索关联

转载 作者:行者123 更新时间:2023-12-01 17:15:54 25 4
gpt4 key购买 nike

我将rails_admin 与globalize3 一起使用,但无法使可搜索关联发挥作用。以下是模型(人员 has_one/belongs_to 姓名 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 正在 people 中查找名为 name_translations.last_name 的列,而不是加入/包含 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中完成吗?感谢您的帮助...

最佳答案

来自this thread ,我遵循了尼克·罗斯福的建议,它对我的​​案子起了作用

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

我尝试了可搜索房间::name,但它不起作用,但可搜索[{Room => :name}]似乎可以正常工作。

关于rails-admin - Rails_admin 可搜索关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13529634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com