Skip to content
Browse files

For Sorting with associated model attributes there is no need of writ…

…ing scope.

example.

column("Country", :country, :sortable => "country.name")
#written in users.rb (user belongs_to country)
  • Loading branch information...
1 parent 223ead4 commit 90492f83635317261ecd49ae12a8afdda89fdc3b Abdulbasit committed Nov 5, 2011
Showing with 10 additions and 5 deletions.
  1. +10 −5 lib/active_admin/resource_controller/collection.rb
View
15 lib/active_admin/resource_controller/collection.rb
@@ -48,15 +48,20 @@ def sort_order(chain)
params[:order] ||= active_admin_config.sort_order
if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
column = $1
- order = $2
- table = active_admin_config.resource_table_name
- table_column = (column =~ /\./) ? column : "#{table}.#{column}"
-
- chain.order("#{table_column} #{order}")
+ order = $2
+ table = active_admin_config.resource_table_name
+ if column =~ /\./
+ assoc_table_and_column = column.split(".")
+ column = assoc_table_and_column[0].pluralize+ "." + assoc_table_and_column[1]
+ chain.includes(assoc_table_and_column[0].to_sym).order("#{column} #{order}")
+ else
+ chain.order("#{table}.#{column} #{order}")
+ end
else
chain # just return the chain
end
end
+
end

0 comments on commit 90492f8

Please sign in to comment.
Something went wrong with that request. Please try again.