Skip to content

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

Description

@StephenVNelson

Mobility Rails 8.0 Compatibility Issue - GitHub Issue Template

Environment

  • Mobility version: 1.3.2
  • Rails version: 8.0.3
  • Ruby version: 3.3.3
  • Database: PostgreSQL

Description

The Rails 8.0 compatibility fix introduced in Mobility 1.3.2 (referencing PR #654 and #655) contains an incomplete implementation that causes a NoMethodError when select_values contains plain strings instead of Arel nodes.

Error

NoMethodError: undefined method `right' for an instance of String

Root Cause

The select_for_count method in lib/mobility/plugins/active_record/query.rb assumes all values in select_values respond to .right and .left methods (i.e., are Arel::Nodes::As objects):

if select_values.any? { |value| value.right.start_with?(ATTRIBUTE_ALIAS_PREFIX) }
  filtered_select_values = select_values.map do |value|
    value.right.start_with?(ATTRIBUTE_ALIAS_PREFIX) ? value.left : value
  end
  # ...
end

However, as noted in PR #654's comments, Rails 8.0 changed behavior (commit: rails/rails@ba468db) such that select_values can contain plain strings like "id" instead of Arel nodes.

Steps to Reproduce

  1. Upgrade to Rails 8.0.x
  2. Use Mobility 1.3.2
  3. Perform any query that triggers select_for_count with a simple select value
  4. Observe the NoMethodError on .right

Proposed Fix

Check if values respond to .right before calling it:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions