Do not depend on the default parameter being set in Person#initialize.

ActiveRecord 5.2.x occasionally calls with a nil parameter explicitly provided, so using default arguments does not work.
This commit is contained in:
Dennis Schubert 2020-02-09 17:38:19 +01:00 committed by Benjamin Neff
parent 93b0e1eb22
commit 25e9728fae
No known key found for this signature in database
GPG key ID: 971464C3F1A90194

View file

@ -185,6 +185,8 @@ class Person < ApplicationRecord
# end
# will not work! The nil profile will be overriden with an empty one.
def initialize(params={})
params = {} if params.nil?
profile_set = params.has_key?(:profile) || params.has_key?("profile")
params[:profile_attributes] = params.delete(:profile) if params.has_key?(:profile) && params[:profile].is_a?(Hash)
super