Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/mobility/plugins/active_record/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,25 @@ def order(opts, *rest)

if ::ActiveRecord::VERSION::MAJOR >= 8
# Fix for https://github.com/shioyama/mobility/pull/654#issuecomment-2503479112
# TODO: Make this better
# When counting with select values that include mobility-translated attributes,
# we need to filter out the aliased columns that Mobility adds (e.g. __mobility_title_en__)
# to prevent errors in the COUNT SQL generation.
def select_for_count
return super unless klass.respond_to?(:mobility_attribute?)

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
# Check if any select values are Mobility attribute aliases
has_mobility_aliases = select_values.any? { |value| value.respond_to?(:right) && value.right.start_with?(ATTRIBUTE_ALIAS_PREFIX) }

# Copied from lib/active_record/relation/calculations.rb
with_connection do |conn|
arel_columns(filtered_select_values).map { |column| conn.visitor.compile(column) }.join(", ")
end
else
super
return super unless has_mobility_aliases

# Filter out Mobility aliases, replacing them with the underlying columns
filtered_select_values = select_values.map do |value|
value.respond_to?(:right) && value.right.start_with?(ATTRIBUTE_ALIAS_PREFIX) ? value.left : value
end

# Copied from lib/active_record/relation/calculations.rb
with_connection do |conn|
arel_columns(filtered_select_values).map { |column| conn.visitor.compile(column) }.join(", ")
end
end
end
Expand Down