ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Custom scope within grouping does not apply filter

Open thisismydesign opened this issue 3 years ago • 4 comments

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?

thisismydesign avatar Jun 09 '22 15:06 thisismydesign

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?

Roguelazer avatar Mar 18 '24 23:03 Roguelazer

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 avatar Mar 19 '24 16:03 sudiptam123

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

sudiptam123 avatar Mar 21 '24 18:03 sudiptam123

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

devsheva avatar Apr 30 '24 23:04 devsheva