Skip to content

Add method to field for sorting an association #1395

Open
fingermark opened this Issue Nov 14, 2012 · 11 comments

9 participants

@fingermark

Rails_admin has correctly detected my belongs to association. Unfortunately, I do not see a way to sort/order these elements. As a result, I have a select box of states that are not ordered by state name.

I saw someone else ask a question on Google Groups on how to sort this association. No replies.

@fingermark

It's really bugging me that I can't figure this out. Is there a short term solution I could use until something like this gets added?

@fingermark

Also, maybe add the ability to specify which field (or method) to display as the value.

@bbenezech bbenezech closed this Nov 15, 2012
@fingermark

Thanks. I actually tried that, but could never get it to work. I'd output the results to the log file and see the elements sorted. But the rendered dropdown/autocomplete still had them unsorted. Here's the code I used:

config.model 'Location' do
  configure :state do
    associated_collection_cache_all false  # REQUIRED if you want to SORT the list as below
    associated_collection_scope do
      location = bindings[:object]
      Proc.new { |scope|
        # scoping all Players currently, let's limit them to the team's league
        # Be sure to limit if there are a lot of Players and order them by position
        scope = scope.where(state_id: location.state_id) if location.present?
        scope = scope.reorder('states.name ASC') # REorder, not ORDER
        Rails.logger.info scope.inspect
        scope
      }
    end
  end
end

Also, how does it know what field to render for the option text?

I think something like options_from_collection_for_select would be nice instead.

@kbaum
kbaum commented Jan 3, 2013

I am getting undefined method associated_collection_scope

https://gist.github.com/4444026

Using rails_admin 0.3

@scottschulthess

@fingermark @bbenezech Could we reopen this?

I discovered why sorting using associated_collection_scope doesn't work

It's because of this line

https://github.com/sferik/rails_admin/blob/master/app/controllers/rails_admin/main_controller.rb#L135

When it builds the association, after attaching the user-defined collection scope, it then calls get_sort_hash, which always ends up overriding the user-defined sorting proc.

As far as how to fix it, I'm not sure.

@mshibuya mshibuya reopened this Jul 3, 2013
@andrewculver

I'm also affected by this issue.

@andrewculver

I've worked around this issue with the solution in these two commits. Please let me know if this is a solution you'd like me to submit a pull request for. (I'd have to make sure this change works with the most current release, as these changes are to an earlier version of rails_admin.)

andrewculver@6224e50
andrewculver@a8929bb

@sasselin

I don't understand how your fix works? I want to use it!

@LucasAU
LucasAU commented Apr 8, 2015

A simpler option is working for me - not sure if it's general enough to work for you, but it might be worth a try. I was looking over the get_sort_hash private method in the MainController (https://github.com/sferik/rails_admin/blob/master/app/controllers/rails_admin/main_controller.rb#L51) and noticed it was looking at model_config.list.sort_by.to_s.

So what worked for me was:
(a) no change in the configuration of the Location model (ie. no associated_collection_cache_all or associated_collection_scope config)
(b) configure the State model to have a sort_by defined under list, ie:

config.model 'State' do
  list do
    sort_by :name
  end
end

This will also effect the default ordering when looking at the State list, but that's probably not a bad thing.

@monkseal
monkseal commented Jun 5, 2015

@LucasAU Thank you! just wanted to confirm your solution works.
Mine came out as:

config.model 'Organization' do
  list do
    sort_by :name
    field :id
    field :name
    field :email
    field :phone
    field :website
  end
end

Not entirely related but I was able to get rid of the 30 record limit by making a change to the configuration of the field

field :organization do
  associated_collection_cache_all false
  associated_collection_scope do
    Proc.new { |scope|
      # returning the full scope prevents the limit from being applied
      scope
     }
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.