def to_arel_sql(value)
      case value
      when Arel::Node, Arel::Nodes::SqlLiteral, nil
        value
      when ActiveRecord::Relation
        Arel.sql(value.spawn.to_sql)
      else
        Arel.sql(value.respond_to?(:to_sql) ? value.to_sql : value.to_s)
      end
    end
def to_arel_sql(value)
      case value
      when Arel::Node, Arel::Nodes::SqlLiteral, nil
        value
      when ActiveRecord::Relation
        Arel.sql(value.spawn.to_sql)
      else
        Arel.sql(value.respond_to?(:to_sql) ? value.to_sql : value.to_s)
      end
    end
Converts a potential subquery into a compatible Arel SQL node.

 Note:
 We convert relations to SQL to maintain compatibility with Rails 5.[0/1].
 Only Rails 5.2+ maintains bound attributes in Arel, so its better to be safe then sorry.
 When we drop support for Rails 5.[0/1], we then can then drop the '.to_sql' conversation