I am working on a Rails application and encountered an issue with the locking_enabled?
method in ActiveRecord. Instead of returning a boolean as expected, it is returning an ActiveRecord::ConnectionAdapters::PostgreSQL::Column
object.
Debugging the method reveals:
lock_optimistically => true
locking_column => "lock_version"
columns_hash.keys => ["id", "....", "....", "created_at", "updated_at", "lock_version"]
columns_hash[locking_column] => #<ActiveRecord::ConnectionAdapters::PostgreSQL::Column:0x.....
The issue seems to be caused by the &&
operator in the locking_enabled?
method, which returns the second operand (columns_hash[locking_column]
) when lock_optimistically
is truthy, instead of a strict boolean.
To fix this, I overwrote the method to explicitly return a boolean:
def locking_enabled?
!!(lock_optimistically && columns_hash[locking_column])
end
Questions:
- Is this behavior a bug in Rails, or is it expected?
- Is overwriting the method with the
!!
approach the best solution, or is there a better alternative (e.g., using.include?
)?
Using:
- Rails version: 7.2.2
- Database: PostgreSQL
- Ruby version: 3.3.4
Any insights or recommendations would be greatly appreciated!