activerecord-hackery / ransack

Object-based searching.
https://activerecord-hackery.github.io/ransack/
MIT License
5.67k stars 807 forks source link

Custom scope within grouping does not apply filter #1339

Open thisismydesign opened 2 years ago

thisismydesign commented 2 years ago

This query simply ignores custom_scope.

Model.ransack({ groupings: [{custom_scope: 'bla'}]}).result.to_sql

Scope is defined as:

scope :custom_scope, ->(name) { ... }

def self.ransackable_scopes(_auth_object = nil)
  [:custom_scope]
end

Possibly related to https://github.com/activerecord-hackery/ransack/issues/653 but here it's silently doing a wrong behavior instead of raising an error.

This sadly breaks activeadmin_addons's Ajax search integration when using custom scopes.

Is there a way to work around this?

Roguelazer commented 7 months ago

This is also breaking a use-case over here. Quick/reproducible demo:

Here's another minimal scope definition for reproducing:

scope :example_scope, ->(value) do
  where(id: value)
end

And here's some example interaction:

> puts SavedGrant.example_scope(2).to_sql
SELECT "saved_grants".* FROM "saved_grants" WHERE "saved_grants"."id" = 2

> puts SavedGrant.ransack({example_scope: 2}).result.to_sql
SELECT "saved_grants".* FROM "saved_grants" WHERE "saved_grants"."id" = 2

> puts SavedGrant.ransack(g: [{example_scope: 2}]).result.to_sql
SELECT "saved_grants".* FROM "saved_grants"

# other filters work fine in groupings
> puts SavedGrant.ransack({g: [{id_eq: 3}, {example_scope: 2}], m: "and"}).result.to_sql
SELECT "saved_grants".* FROM "saved_grants" WHERE "saved_grants"."id" = 3

Looking at the code, it looks sort of like scopes are "bolted on" after everything else (see add_scope in search.rb, and I don't see a very straightforward way to add support for them to the core Condition class. Maybe I'm wrong?

sudiptam123 commented 7 months ago

I have a similar query added for select-search filter as here. Pls clarify on this. The end result is that, results aren't filtered in the way it is expected to be.

sudiptam123 commented 7 months ago

I have a similar query added for select-search filter as here. Pls clarify on this. The end result is that, results aren't filtered in the way it is expected to be.

I found the solution for this issue. Seems upgrading activeadmin-addons from 1.7.x to 1.10.x is working correctly. Hence this issue is resolved

devsheva commented 6 months ago

any updates to this? i think i have the same issue #1490