我正在尝试覆盖新/编辑表单的belongs_to 下拉列表的默认顺序。我想将“belongs_to”关系排序为“name”,而不是默认的“id desc”。
基于维基(https://github.com/sferik/rails_admin/wiki/Associations-scoping)我有这个:
field :theme do
associated_collection_cache_all false
associated_collection_scope do
Proc.new { |scope|
scope = scope.reorder("themes.name ASC")
}
end
end
end
在选取 where 语句时,重新排序似乎被忽略。
最佳答案
您确定要排序的列是“名称”而不是“名称”吗?
调试此问题的一个好方法是打开 Rails 控制台并查看您的重新排序是否确实如此。
Theme.all.reorder("themes.names ASC")
我猜这可能不会按预期工作,您需要调整重新排序。
如果您想查看它正在创建的 SQL,您可以这样做。
Theme.all.reorder("themes.names ASC").to_sql
这可能会为您提供有关问题所在的更多信息。
关于ruby-on-rails - rails_admin编辑belongs_to排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18196127/