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

sort_link on polymorphic association #421

Closed
glaucocustodio opened this issue Aug 22, 2014 · 10 comments
Closed

sort_link on polymorphic association #421

glaucocustodio opened this issue Aug 22, 2014 · 10 comments

Comments

@glaucocustodio
Copy link
Contributor

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.

@mwalsher
Copy link

+1

@eneagoe
Copy link
Contributor

eneagoe commented Nov 10, 2014

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

@jonatack
Copy link
Contributor

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 😃

@eneagoe
Copy link
Contributor

eneagoe commented Nov 10, 2014

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.

@jonatack
Copy link
Contributor

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.

eneagoe added a commit to eneagoe/ransack that referenced this issue Nov 10, 2014
@jonatack
Copy link
Contributor

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

@eneagoe
Copy link
Contributor

eneagoe commented Nov 10, 2014

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.

jonatack added a commit that referenced this issue Nov 10, 2014
Fixes sort on polymorphic association #421
@jonatack
Copy link
Contributor

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

@dipaktc
Copy link

dipaktc commented Apr 12, 2017

How to sort_link with multiple polymorphic relationship ?
#782

@scarroll32
Copy link
Member

Closing as a workaround exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants