Skip to content

filter by e.g. notes_count_eq does not work #43

@fluxsaas

Description

@fluxsaas

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions