简体   繁体   English

rails_admin编辑belongs_to排序

[英]rails_admin edit belongs_to ordering

I'm trying to override the default ordering of the belongs_to drop-downs for the new/edit forms. 我正在尝试覆盖新/编辑表单的belongs_to下拉列表的默认顺序。 I'd like to order the belongs_to relationships to name instead of the default id desc. 我想将belongs_to关系命名为name而不是默认的id desc。

Based on the wiki ( https://github.com/sferik/rails_admin/wiki/Associations-scoping ) I have this: 基于wiki( 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

The reorder seems to be ignored while where statements are picked up. 在拾取语句的地方,似乎忽略了重新排序。

Are you sure the column you're trying to sort by is 'names' and not 'name'? 您确定要排序的列是“名称”而不是“名称”吗?

A good way to debug this would be to open a rails console and see if your reorder actually works this way. 调试这个的一个好方法是打开一个rails控制台,看看你的重新排序实际上是否正常工作。

Theme.all.reorder("themes.names ASC")

I'm guessing that probably won't work as expected and you need to tweak the reorder. 我猜这可能不会按预期工作,你需要调整重新排序。

If you want to see the SQL it's creating, you can do this. 如果你想看到它正在创建的SQL,你可以这样做。

Theme.all.reorder("themes.names ASC").to_sql

That might give you more information about whats going wrong. 这可能会为您提供有关出错的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM