rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
972 stars 559 forks source link

Fix enums defined on string columns #1059

Closed mgrunberg closed 1 year ago

mgrunberg commented 1 year ago

Built on top of #1058 to run specs

Fixes https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/1042

varchar, varchar_max and text sql server types extends from char (ActiveRecord::ConnectionAdapters::SQLServer::Type:Char).

ActiveRecord::ConnectionAdapters::SQLServer::Type::Char.serialize return an ActiveRecord::ConnectionAdapters::SQLServer::Type::Data instance.

ActiveRecord::ConnectionAdapters::SQLServer::Type::Data defines

def eql?(other)
  self.class == other.class && value == other.value
end

Let's imagine the following ActiveRecord definition

class MyRecord < ActiveRecord::Base
  enum release: { alpha: "A", beta: "B" }
end

When we call my_record.alpha? it compares release serialized value with the string "A". It will always be false because ActiveRecord::ConnectionAdapters::SQLServer::Type::Data != String.

This commit reimplements eql? to deal with string arguments.

aidanharan commented 1 year ago

Also, the https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1058 changes should be removed from this PR.

mgrunberg commented 1 year ago

@aidanharan done. Please review again