activerecord-hackery/ransack

sort_link on polymorphic association

glaucocustodio opened this issue · 10 comments

Hi guys, I am on Rails 4.1 (Ruby 2.1.2) and ransack branch: 'rails-4.1'.

I have this search field working as expected:
xxxable_of_Ymodel_type_some_attr_gteq

But when I try to sort by the same field:
= sort_link(@q, :xxxable_of_Ymodel_type_some_attr

I get this error: uninitialized constant Model::Xxxable

Sort link does not work with polymorphic association? Thank you.

+1

It looks like sort_link changes the actual reference in the generated link: instead of xxxable_of_Ymodel_type_some_attr it becomes xxxable_of_ymodel_type_some_attr (notice that the name of the model is downcased).
Also, on ransack 1.4.1 it works correctly, but not on later versions.

Hi guys, could you please provide a failing test case and/or code to reproduce this issue as per the Contributing Guide? @eneagoe and @glaucocustodio, it's not clear if you two are reporting the same issue. Please contribute a pull request to fix the issue, or provide the information mentioned in the Contributing Guide so that someone else can reproduce it without spending time guessing what the exact issue is. If neither one happens in the next 2 weeks, we'll close the issue as stale/unactionable. Thanks for your help 😃

I'll try to see if I can write a failing test case, but meanwhile I can point out where the problem comes from: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/helpers/form_helper.rb#L86
Getting rid of the downcase will solve the problem, and no tests are failing. I'm not sure why it's there.

Thanks @eneagoe, that would be great. Git blame shows that change happened in bd22760 when sorting by multiple fields was added. We definitely need a test for this case.

@glaucocustodio's issue seems to be a different one, however, because the change mentioned by @eneagoe was not committed in the rails-4.1 branch and @glaucocustodio doesn't report uppercase being converted to lowercase.

Actually, that might be correct.
My pull request fixes only one of the issues related to this kind of sorting, but there's another one: it seems that when using a sort_link on a polymorphic association we have to specify the name of the link explicitly
= sort_link(@q, :xxxable_of_Ymodel_type_some_attr, 'Attr') will work, but
= sort_link(@q, :xxxable_of_Ymodel_type_some_attr will not, and will generate a uninitialized constant Model::Xxxable error.

@eneagoe if that is a reliable workaround, would you mind adding it to the readme and wiki?

To begin looking for a solution, a failing spec would be a good place to start.

How to sort_link with multiple polymorphic relationship ?
#782

Closing as a workaround exists.