简体   繁体   中英

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. I'd like to order the belongs_to relationships to name instead of the default id desc.

Based on the wiki ( https://github.com/sferik/rails_admin/wiki/Associations-scoping ) I have this:

 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.

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.

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

That might give you more information about whats going wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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