Skip to content

Conversation

@StephenVNelson
Copy link

@StephenVNelson StephenVNelson commented Oct 26, 2025

Description

Fixes a bug in select_for_count (Rails 8.0+) where calling .right on
select_values crashes when the array contains plain strings instead of
Arel nodes.

The Problem

When using Mobility with Rails 8.0+ and calling .count on a query with
selected columns, the code assumes all select_values are Arel::Nodes::As
objects with a .right method. However, select_values can contain a mix of
plain strings (like "id") and Arel nodes, causing:

Article.select(:id).count
# NoMethodError: undefined method `right' for "id":String

The Solution

Added respond_to?(:right) checks before calling .right on select_values
to safely handle both strings and Arel nodes.

Changes Made

  • ✅ Added respond_to?(:right) checks in select_for_count method
  • ✅ Improved inline documentation explaining the fix
  • ✅ Added 3 regression tests covering different scenarios

Test Results

All existing tests pass, plus 3 new tests:

  • ✅ Handles count with regular column in select (reproduces original bug)
  • ✅ Handles count with translated column in select
  • ✅ Handles count without select when mobility is present
# Rails 8.0 with Ruby 3.2.5
15 examples, 0 failures

Related Issues

Closes #678
Related to

  1. Fixes for ActiveRecord 8.0 #654 (comment)
  2. Fix for Rails 8 query issue breaks select with no order clause #659 (comment)

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.

Rails 8.0: NoMethodError: undefined method 'right' for an instance of String in select_for_count

1 participant