ransack
ransack copied to clipboard
Sorting on globalized attributes of associations breaks when joining translations
I use the globalize gem for managing internationalized attributes of models. When I try to sort on the translated attribute of an association and add a custom join to the translations of the main model (as is also done when using Globalize's with_translation
scope), then the sorting breaks. Ransack doesn't add the join to association's translations table and order on the association's translated attribute any longer.
So suppose you have a Book with a translated title attribute, and each Book has a Category with a translated name attribute. The following doesn't work any longer: Book.joins(:translations).ransack({ s: ['category_translations_name asc'] }).result
It yields the following query: SELECT \"books\".* FROM \"books\" INNER JOIN \"book_translations\" ON \"book_translations\".\"book_id\" = \"books\".\"id\" LEFT OUTER JOIN \"categories\" ON \"categories\".\"id\" = \"books\".\"category_id\"
See for more details and examples the following gist test: https://gist.github.com/kreintjes/a85b610afcfc87f4f680b874098a2e01
This worked in version 1.8.7 of Ransack, but stopped working when updating to Ransack 2. I believe the problem is caused by the change introduced in https://github.com/activerecord-hackery/ransack/pull/937. When reverting this change two of the three failing cases pass, while the last fails with a different error.
Might not be Globalize related, but potentially also happens when sorting on an association of an association while joining an association (with the same name).
Versions Ruby 2.4.1 Rails 5.0.4 Ransack 2.0.1 Globalize 5.1.0
In rails 5.1 and 5.2 this isn't a problem any longer, so it appears Ransack 2.x doesn't fully support Rails 5.0.
It would be good to have some Globalize expertise to help with the gem, perhaps write some tests with a dummy app.
@kreintjes Rails 5.0 is not supported by the Rails team, when I released Ransack 2.0, I just added that version since the specs are passing with it, so I thought if it won't require any time to make it work, why not support it. But if it takes considerable time for us to make this work, I would rather say no and focus on other issue. Having said that, PRs are always welcomed, so if you feel like you want to fix this and submit one, feel free to do.
I would also highly recommend to upgrade your app to 5.1 at least and in the meantime, if you are using Ransack 1.8.7 you are not really losing anything, since 2.0 is more of a cleanup version, there are not much new exciting features.
@gregmolnar We have updated our app to Rails 5.2, so for us it's no longer a problem, but it's possible other users run into this. Maybe it would be wise to indicate Rails 5.0 isn't fully supported by Ransack 2.0 on the readme and/or increase the minimum required Rails version in the gemspec. At least there is this issue now, so if people search for it they see it's a known problem and see the potential solutions here (stay at Ransack 1.8.7 or upgrade to Rails 5.1+). Anyway, no fix required on my end and I'm fine with closing this issue. Thank you for your response.
Ps. Rails 5.0 is still supported by the Rails team, at least for major security issues, see: https://rubyonrails.org/security/.
Thanks everyone for your inputs. A related issue on Ransack is community engagement: the gem is widely used and is quite complex, and I understand frustration at features which are unclear or not working.
At the same time, more community contributions on issues or even helping with documentation and writing additional test cases (for closed issues which don't have proper coverage), would really be appreciated.
Hey, I'm reopening this, because in Rails 6.0 it's a problem if I want to present the name
from the translation table as a first-class property of the model (let's say company
) using the ransack_alias
method.
class Company
translates :name
ransack_alias :name, :translations_name
end
After doing this, filtering works great when just supplying the name in the query parameter, but sorting doesn't. This still requires the sorting field is named translations_name
.
@ekampp does this issue only occur in conjunction with the globalize gem?
@seanfcarroll, yes.
Any chance of making a PR to fix it?
Not super inclined at the moment. We're moving away from Ransack.
Thanks for the reply @ekampp
Why are you moving away, out of interest?
We're moving our data onto a graph database, so it's no longer a good fit.
Fair enough. Good luck with it!
I faced same issue with sorting. this work for me perfectly:
<%= sort_link @search, :translations_name %>