-
Notifications
You must be signed in to change notification settings - Fork 66
Description
Hello!
filtering by counter cache (e.g. notes_count
) columns on the model does not seem to work.
Expected Behavior
it should work :)
Actual Behavior
it does not work.
Steps to Reproduce the Problem
see:
- code: master...easyPEP:debfec8b9078403e8164b06faf9c41092d82d05c
- Failing CI Run: https://github.com/easyPEP/jsonapi.rb/runs/2076608122?check_suite_focus=true
Update#1:
problematic is the method
Ransack::Predicate.detect_and_strip_from_string!(field_name)
which also detects and extracts ["count", "_count"]
from a string. So this is likely more a ransack issue.
more info:
detect_and_strip_from_string
uses the method predicates.sorted_names_with_underscores
to match and extracts predicates from a string. see: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/predicate.rb#L20
Update#2:
we can mitigate this problem, because we have the allowed_fields
value, which enables us to stop picking apart the requested_field
string:
while Ransack::Predicate.detect_from_string(field_name).present? do
# break if we have an exact match with an allowed_fields
# allowing us to filter by e.g. by counter cache attributes like `notes_count`
break if allowed_fields.include?(field_name)
predicate = Ransack::Predicate
.detect_and_strip_from_string!(field_name)
predicates << Ransack::Predicate.named(predicate)
end
Update#3:
with the approach from Update#2, i fixed the failings specs.