Skip to content

Fix deprecation warning and correctness bug when using arel_extensions >= 2.2#1679

Open
javieromar7 wants to merge 1 commit into
activerecord-hackery:mainfrom
javieromar7:fix/extract-correlated-key-arel-extensions-compatibility
Open

Fix deprecation warning and correctness bug when using arel_extensions >= 2.2#1679
javieromar7 wants to merge 1 commit into
activerecord-hackery:mainfrom
javieromar7:fix/extract-correlated-key-arel-extensions-compatibility

Conversation

@javieromar7

Copy link
Copy Markdown

Problem

When arel_extensions >= 2.2 is present, extract_correlated_key triggers a deprecation warning on every query involving correlated joins:

DEPRECATION WARNING: `ArelExtensions::Attributes#==` is now deprecated. Use `.eq` instead.
(called from extract_correlated_key at lib/ransack/adapters/active_record/context.rb:215)

Beyond the warning, there is a silent correctness bug: ArelExtensions >= 2.2 overrides == on Arel attributes to return an Arel::Nodes::Equality SQL predicate node — not a boolean. This means the if join_root.left == pk check is always truthy (an Arel node is never nil/false), so join_root.right is returned unconditionally, regardless of whether join_root.left is actually the primary key.

Root cause

# arel_extensions/lib/arel_extensions/attributes.rb
def ==(other)
  deprecated 'Use `.eq` instead.'
  Arel::Nodes::Equality.new self, Arel.quoted(other, self)  # returns an Arel node, not a boolean
end

Fix

Replace == with eql? for the primary key identity check. Arel::Attributes::Attribute#eql? performs value-based comparison (name + relation) without going through the ArelExtensions override.

# Before
if join_root.left == pk
# After
if join_root.left.eql?(pk)

Verification

pk  = User.arel_table[:id]
pk2 = User.arel_table[:id]

pk.equal?(pk2)  # => false (different objects)
pk.eql?(pk2)    # => true  (same name + relation)
pk == pk2       # => Arel::Nodes::Equality (always truthy — the bug)

…ated_key

ArelExtensions >= 2.2 overrides  on Arel attributes to build SQL
predicate nodes (Arel::Nodes::Equality) instead of returning a boolean.
This caused two problems in extract_correlated_key:

1. A DEPRECATION WARNING on every query involving correlated joins.
2. A silent correctness bug: the comparison always returned truthy
   (an Arel node is never nil/false), so join_root.right was always
   returned regardless of whether join_root.left was actually the PK.

Replace  with , which performs value-based comparison on
Arel::Attributes::Attribute objects (comparing name and relation) without
going through the ArelExtensions override.
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.

1 participant