Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting by an association's field #13

Closed
aq2-sanger opened this Issue Aug 10, 2011 · 10 comments

Comments

Projects
None yet
8 participants
@aq2-sanger
Copy link
Contributor

aq2-sanger commented Aug 10, 2011

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?

@ernie

This comment has been minimized.

Copy link
Member

ernie commented Aug 14, 2011

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

@ernie ernie closed this Aug 14, 2011

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 9, 2012

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!

@aq2-sanger

This comment has been minimized.

Copy link
Contributor Author

aq2-sanger commented Feb 9, 2012

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

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 9, 2012

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

@4ydin

This comment has been minimized.

Copy link

4ydin commented Nov 12, 2013

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!

@tylercollier

This comment has been minimized.

Copy link

tylercollier commented Dec 17, 2014

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

@jonatack

This comment has been minimized.

Copy link
Member

jonatack commented Jan 14, 2015

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

@dgm

This comment has been minimized.

Copy link

dgm commented Jan 27, 2015

@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')

@jonatack

This comment has been minimized.

Copy link
Member

jonatack commented Jan 27, 2015

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

jonatack referenced this issue Aug 21, 2015

[skip ci] Change associations `sort_link` docs
and provide the 'SQL string' version as an alternative in case of
difficulty.

This commit is in response to some users over time stating that the
docs were incorrect. I haven't been able to reproduce the issue. Let's
see what user feedback comes in. Closes #468, #450 and #431.
@sauloperez

This comment has been minimized.

Copy link

sauloperez commented Jan 25, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.