From 46c1b2c37deb290fe1711a038273dee4aa66d28f Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 29 Aug 2021 17:46:04 -0500 Subject: [PATCH] Fix #4868: undefined method `to_string' errors in /user_events search Fix a regression introduced in rails/rails@4b1122c with the upgrade to Rails 6.1.4.1. Triggered by a call to `SELECT * FROM ip_geolocations WHERE ip_addr in ...`. A Rails refactoring changed the way that `WHERE ... IN ...` statements worked, which had the side effect of passing a string value to our IpAddressType serializer where before we expected a Danbooru::IpAddress object. --- app/logical/ip_address_type.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/logical/ip_address_type.rb b/app/logical/ip_address_type.rb index f3921e727..e633be55b 100644 --- a/app/logical/ip_address_type.rb +++ b/app/logical/ip_address_type.rb @@ -7,15 +7,24 @@ require "active_record/connection_adapters/postgresql_adapter" # # @see config/initializers/types.rb # @see https://www.bigbinary.com/blog/rails-5-attributes-api +# @see https://api.rubyonrails.org/classes/ActiveModel/Type/Value.html class IpAddressType < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Inet + # Cast a String (or nil) value from the database to a Danbooru::IpAddress object. + # # @param value [String] the IP address from the database + # @return [Danbooru::IpAddress] def cast(value) return nil if value.blank? super(Danbooru::IpAddress.new(value)) end - # @param [Danbooru::IpAddress] the IP address object + # Serialize a Danbooru::IpAddress to a String for the database. + # XXX May be passed a string in some situations? + # + # @param value [Danbooru::IpAddress] the IP address object + # @return [String] def serialize(value) - value&.to_string + return value.to_string if value.is_a?(Danbooru::IpAddress) + super value end end