Skip to content

Check type before sending #value message to predicate#1468

Merged
scarroll32 merged 3 commits intoactiverecord-hackery:mainfrom
spaghetticode:fix-casted_array-method
Sep 24, 2025
Merged

Check type before sending #value message to predicate#1468
scarroll32 merged 3 commits intoactiverecord-hackery:mainfrom
spaghetticode:fix-casted_array-method

Conversation

@spaghetticode
Copy link
Copy Markdown
Contributor

Closes #1467

The previous implementation was giving for granted that every predicate respond to #value, which doesn't seem to be the case at least when having a Arel::SelectManager.

by simply inverting the terms of the existing AND check we can fix the issue without introducing unknown side effects.

spaghetticode added a commit to peterberkenbosch/solidus that referenced this pull request Dec 15, 2023
Issue: activerecord-hackery/ransack#1467
PR: activerecord-hackery/ransack#1468

Hopefully the PR will be merged soon so we can remove this patch.
@spaghetticode
Copy link
Copy Markdown
Contributor Author

Interestingly, CI is not running on the PR 🤷

elia pushed a commit to peterberkenbosch/solidus that referenced this pull request Dec 20, 2023
Issue: activerecord-hackery/ransack#1467
PR: activerecord-hackery/ransack#1468

Hopefully the PR will be merged soon so we can remove this patch.
elia pushed a commit to peterberkenbosch/solidus that referenced this pull request Dec 20, 2023
Issue: activerecord-hackery/ransack#1467
PR: activerecord-hackery/ransack#1468

Hopefully the PR will be merged soon so we can remove this patch.
@scarroll32
Copy link
Copy Markdown
Member

Interestingly, CI is not running on the PR 🤷

Needs to be approved for the 1st run.

@scarroll32
Copy link
Copy Markdown
Member

@spaghetticode could you please add a test for this change?

The previous implementation was giving for granted that every predicate
respond  to `#value`, but that doesn't seem to be the case at least when
having a `Arel::SelectManager`.

by simply inverting the terms of the existing AND check we can fix the issue
without introducing unknown side effects.

In order to test the change, a new ransacker has been added to the Person model.
@spaghetticode spaghetticode force-pushed the fix-casted_array-method branch from e72e954 to e7c8250 Compare January 8, 2024 16:49
@spaghetticode
Copy link
Copy Markdown
Contributor Author

@scarroll32 I've just added a test that fails without the change.

The method I modified is private, so it needs to be tested indirectly. I followed the existing examples, but I had to add a new ransacker to the Person model, I hope it makes sense. Please let me know if it works for you, thanks!

MadelineCollier added a commit to MadelineCollier/solidus that referenced this pull request Aug 7, 2024
Solidus core's gemspec already required that ransack be '~> 4.0', but
the latest version of ransack, v4.2.0, released July 10 2024, introduces
a bug. The previous implementation was taking for granted that every
predicate would respond to #value, which doesn't seem to be the case
when the predicate is an instance of a Arel::SelectManager.

This has already been flagged by @spaghetticode in his PR against
ransack: activerecord-hackery/ransack#1468

Since there has been little movement on this PR since January, we should
lock to a version that works for us since currently many of our product
specs are failing. (eg. spec/models/spree/product_spec.rb:659)

We can remove this lock once the PR is merged and once the above test
(and the others that are failing) are able to pass in ransack v4.2.0 or
subsequent versions.
@MadelineCollier
Copy link
Copy Markdown

I am also having this issue, will there be any movement on this PR @scarroll32 ?

NoMethodError:
       undefined method `value' for #<Arel::SelectManager:0x000000010b0fb3b8 @ast=#<Arel::Nodes::SelectStatement:0x000000010b0fb390 @cores.........



               predicate.value.is_a?(Array) && predicate.is_a?(Arel::Nodes::Casted)
                        ^^^^^^
     # ./spec/models/spree/product_spec.rb:667:in `block (3 levels) in <top (required)>'

Finished in 28 minutes 11 seconds (files took 4.77 seconds to load)
1 example, 1 failure

For now I have locked my gemfile to ransack 4.1.1 and specs pass on that version.

MadelineCollier added a commit to MadelineCollier/solidus that referenced this pull request Aug 8, 2024
Solidus core's gemspec already required that ransack be '~> 4.0', but
the latest version of ransack, v4.2.0, released July 10 2024, introduces
a bug. The previous implementation was taking for granted that every
predicate would respond to #value, which doesn't seem to be the case
when the predicate is an instance of a Arel::SelectManager.

This has already been flagged by @spaghetticode in his PR against
ransack: activerecord-hackery/ransack#1468

Since there has been little movement on this PR since January, we should
lock to a version that works for us since currently many of our product
specs are failing. (eg. spec/models/spree/product_spec.rb:659)

We can remove this lock once the PR is merged and once the above test
(and the others that are failing) are able to pass in ransack v4.2.0 or
subsequent versions.
tvdeyen pushed a commit to tvdeyen/solidus that referenced this pull request Aug 27, 2024
Solidus core's gemspec already required that ransack be '~> 4.0', but
the latest version of ransack, v4.2.0, released July 10 2024, introduces
a bug. The previous implementation was taking for granted that every
predicate would respond to #value, which doesn't seem to be the case
when the predicate is an instance of a Arel::SelectManager.

This has already been flagged by @spaghetticode in his PR against
ransack: activerecord-hackery/ransack#1468

Since there has been little movement on this PR since January, we should
lock to a version that works for us since currently many of our product
specs are failing. (eg. spec/models/spree/product_spec.rb:659)

We can remove this lock once the PR is merged and once the above test
(and the others that are failing) are able to pass in ransack v4.2.0 or
subsequent versions.

(cherry picked from commit 1664d10)

# Conflicts:
#	core/solidus_core.gemspec
@scarroll32 scarroll32 requested a review from Copilot September 24, 2025 18:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where Ransack was incorrectly assuming all predicates respond to the #value method, which causes issues with certain Arel objects like Arel::SelectManager. The fix reorders the type checking in the casted_array? method to ensure type safety before method calls.

  • Reorders boolean logic in casted_array? method to check type before calling #value
  • Adds a new ransacker for article_tags that uses Arel::SelectManager
  • Adds test coverage for HABTM associations using the new ransacker

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
lib/ransack/nodes/condition.rb Fixes the type checking order in casted_array? method
spec/support/schema.rb Adds new article_tags ransacker that returns Arel::SelectManager
spec/ransack/adapters/active_record/base_spec.rb Adds test case for HABTM associations functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread spec/support/schema.rb
@scarroll32 scarroll32 merged commit 340b563 into activerecord-hackery:main Sep 24, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NoMethodError: undefined method `value' for #<Arel::SelectManager

4 participants