Add method to field for sorting an association #1395

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

Comments

Projects
None yet
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

This comment has been minimized.

Show comment
Hide comment
@fingermark

fingermark Nov 15, 2012

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?

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

This comment has been minimized.

Show comment
Hide comment
@fingermark

fingermark Nov 15, 2012

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

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

@bbenezech bbenezech closed this Nov 15, 2012

@fingermark

This comment has been minimized.

Show comment
Hide comment
@fingermark

fingermark Nov 15, 2012

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.

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

This comment has been minimized.

Show comment
Hide comment
@kbaum

kbaum Jan 3, 2013

I am getting undefined method associated_collection_scope

https://gist.github.com/4444026

Using rails_admin 0.3

kbaum commented Jan 3, 2013

I am getting undefined method associated_collection_scope

https://gist.github.com/4444026

Using rails_admin 0.3

@scottschulthess

This comment has been minimized.

Show comment
Hide comment
@scottschulthess

scottschulthess Jul 2, 2013

@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.

@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

This comment has been minimized.

Show comment
Hide comment
@andrewculver

andrewculver Sep 18, 2013

Contributor

I'm also affected by this issue.

Contributor

andrewculver commented Sep 18, 2013

I'm also affected by this issue.

@andrewculver

This comment has been minimized.

Show comment
Hide comment
@andrewculver

andrewculver Sep 20, 2013

Contributor

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.)

https://github.com/andrewculver/rails_admin/commit/6224e50ce72062455c2602b85886d3269bcf3f2d
https://github.com/andrewculver/rails_admin/commit/a8929bb294168c5c7f081710ddb412c5f4cbdeff

Contributor

andrewculver commented Sep 20, 2013

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.)

https://github.com/andrewculver/rails_admin/commit/6224e50ce72062455c2602b85886d3269bcf3f2d
https://github.com/andrewculver/rails_admin/commit/a8929bb294168c5c7f081710ddb412c5f4cbdeff

@sasselin

This comment has been minimized.

Show comment
Hide comment
@sasselin

sasselin Mar 19, 2014

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

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

@lucas-nelson

This comment has been minimized.

Show comment
Hide comment
@lucas-nelson

lucas-nelson 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.

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

This comment has been minimized.

Show comment
Hide comment
@monkseal

monkseal 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

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