Ransack: Sorting by an association's field

Created on 10 Aug 2011  路  10Comments  路  Source: activerecord-hackery/ransack

I would like to be able to, e.g., pass in 'sorts = "user_name asc"' to sort by the user's name field.

Shouldn't be too hard to implement, should it? Checking the logs, the join to the associated table already gets performed, so I'm assuming it'd be trivial to pass in the association's field name in the order(...) call as well?

Most helpful comment

I think you mean 'location_name', not 'location.name'

All 10 comments

This actually already works, if you set sorts to a string, it's handled the way you mention. I also added sort_link today.

I can't quite seem to get sorting to work by an associated record's field. Is it possible, and if so, what am I missing?

Relevant Models
class Vehicle < ActiveRecord::Base
  belongs_to :location
end

class Location < ActiveRecord::Base
  has_many :vehicles
  validates_presence_of :name
end
Sort Links

This works fine:

%th= sort_link @q, :location_id, Vehicle.human_attribute_name(:location)

But I'd really like to do this:

%th= sort_link @q, 'location.name', Vehicle.human_attribute_name(:location)

I'd really appreciate any help or guidance! Thanks!

I think you mean 'location_name', not 'location.name'

That's it! So awesome. Thank you!!

sorry for asking here, but would one of you mind helping me about this sort_link?
i have :
ransack_filters = {"s" => ["name asc", "id asc"]}.merge(params["filters"] || {})
@search = query.search(ransack_filters)
@users = @search.result.select("DISTINCT relationships.*")
it looks like ok, there's no error or problem at all. but, when i click the table name (where this sort_link should work), the list would not be sorted. it stays like before. my reference : http://youcanlookitup.wordpress.com/2012/11/20/filter-search-pages-with-rails-sql-views-rans/
and..if i choose one of user to show detail of it (action : show), it gives an error like this : Cannot visit Arel::Nodes::Distinct
i would really appreciate any help..thanks in advance!

I can confirm @aq2-sanger's fix. It's documented in the main README as location.name, but should really be location_name.

It seems to me that assocations should currently be specified as 'locations.name' in the sort link (note the plural table name and not 'location.name' as above) and :location_name pretty much everywhere else (search field, controller, rails console, etc.)

@jonatack No that's not necessarily correct - the sort link string is not a 1-1 mapping to an order clause spelling out sql. It's a relation name, which may be a different table. The documentation had me going totally the wrong direction on this. The association section says,

'Please note that in a sort link, the association is expressed as an SQL string ('employees.last_name') with a pluralized table name, instead of the symbol :employee_last_name syntax with a class#underscore table name used for Ransack objects elsewhere.'

This appears to not be accurate. In my case, I have a Request model that belongs_to a State model, which has a name attribute. I was lead to believe by that statement that I should use:

sort_link(@q, 'states.name')

But found it only worked when I changed it to:

sort_link(@q, 'state_name')

@dgm I am unable to reproduce this. Could you please provide the information mentioned in the Contributing Guide please?

Thanks for clarifying the docs @jonatack!

I wasn't sure if that still worked for a field of an association's association (A -> B-> C.field) and it does. lib/ransack/adapters/active_record/context.rb#L209 seems to be the method responsible for that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanfcarroll picture seanfcarroll  路  4Comments

JFimex picture JFimex  路  3Comments

MatsumotoHiroko picture MatsumotoHiroko  路  4Comments

seanfcarroll picture seanfcarroll  路  4Comments

edgarbarrero picture edgarbarrero  路  4Comments