“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey (contact@robl.me)
Senior Software Engineer, Brighton, UK

Rails 6.0.4 mangled SQL

Looks like Rails 6.0.4 upgrade has mangled the SQL JSONAPI-Resources is constructing.

Rswag::Specs::UnexpectedResponse:
        Expected response code '500' to match '200'
        Response body: {
          "errors": [
            {
              "title": "Internal Server Error",
              "detail": "Internal Server Error",
              "code": "INTERNAL_SERVER_ERROR",
              "status": "500",
              "meta": {
                "exception": "Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.\"id\" AS \"users_id\", users.last_name, users.first_name FROM `users` INNER JOIN `' at line 1",

The backtrace has lead me here.

From: /Users/rl/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/activerecord-6.0.4/lib/active_record/relation/calculations.rb:186 ActiveRecord::Calculations#pluck:

    181: def pluck(*column_names)
    182:   if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
    183:     return records.pluck(*column_names)
    184:   end
    185: 
 => 186:   binding.pry
    187: 
    188:   if has_include?(column_names.first)
    189:     relation = apply_join_dependency
    190:     relation.pluck(*column_names)
    191:   else
    192:     klass.disallow_raw_sql!(column_names)
    193:     relation = spawn
    194:     relation.select_values = column_names
    195:     result = skip_query_cache_if_necessary { klass.connection.select_all(relation.arel, nil) }
    196:     result.cast_values(klass.attribute_types)
    197:   end
    198: end

[1] pry(#<User::ActiveRecord_Relation>)> column_names
=> ["\"users\".\"id\" AS \"users_id\"", "users.last_name", "users.first_name"]
What’s up with that? Looking.