ruby 如果存在键,Rails会用另一个哈希替换一个哈希

ukqbszuj  于 2022-12-18  发布在  Ruby
关注(0)|答案(1)|浏览(121)

我不知道发生了什么,但我有两个哈希值:
first_hash

{
  "SspFileIds"=>[
    "0823c9bf-d41e-4324-80be-3fb2c6ae49f0"
  ],
  "SendEnvelopeDescription"=>
  {
    "CallbackUrl"=>"https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##",
    "WorkstepEventCallback"=>{
      "Url"=>"https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##",
      "WhiteList"=>[
        "string"
      ]
    },
    "Steps"=>
    [
      {
        "OrderIndex"=>1,
        "Recipients"=>[
          {
            "Email"=>"teddy@olson.info",
            "FirstName"=>"Merle",
            "LastName"=>"Stracke",
            "LanguageCode"=>"en"
          }
        ]
      },
      {
        "OrderIndex"=>1,
        "Recipients"=>[
          {
            "Email"=>"zora_gerhold@lemke.co",
            "FirstName"=>"Laine",
            "LastName"=>"Boehm",
            "LanguageCode"=>"en"
          }
        ]
      }
    ]
  }
}

第二个问题:
second_hash

{
  "SspFileIds"=>[
    "7fcf6021-386d-4f31-a871-a89afb8fb36e"
  ],
  "SendEnvelopeDescription"=>{
    "Name"=>"1 Recipient",
    "EmailSubject"=>"Please sign the enclosed envelope",
    "EmailBody"=>"Dear #RecipientFirstName# #RecipientLastName#\n\n#PersonalMessage#\n\nPlease sign the envelope #EnvelopeName#\n\nEnvelope will expire at #ExpirationDate#",
    "DisplayedEmailSender"=>"",
    "EnableReminders"=>true,
    "FirstReminderDayAmount"=>5,
    "RecurrentReminderDayAmount"=>3,
    "BeforeExpirationDayAmount"=>3,
    "DaysUntilExpire"=>28,
    "StatusUpdateCallbackUrl"=>"",
    "LockFormFieldsAtEnvelopeFinish"=>false,
    "Steps"=>[
      {
        "OrderIndex"=>1,
        "Recipients"=>[
          {
            "Email"=>"Placeholder:",
            "FirstName"=>"",
            "LastName"=>"",
            "LanguageCode"=>"",
            "DisableEmail"=>false,
            "AddAndroidAppLink"=>false,
            "AddIosAppLink"=>false,
            "AddWindowsAppLink"=>false,
            "AllowDelegation"=>true,
            "AllowAccessFinishedWorkstep"=>false,
            "SkipExternalDataValidation"=>false,
            "AuthenticationMethods"=>[],
            "IdentificationMethods"=>[]
          }
        ],
        "RecipientType"=>"Signer",
      }],
  }
}

现在我想把first_hash添加到second_hash中,只覆盖两者中存在的键/值,并保持second_hash中的其余部分不变(应该添加到结果中)。
test = first_hash.merge(second_hash)
但结果是令人惊讶的-什么都没有改变,它打印第二哈希不变:

> test
{"SspFileIds"=>["7fcf6021-386d-4f31-a871-a89afb8fb36e"],
 "SendEnvelopeDescription"=>
  {"Name"=>"1 Recipient",
   "EmailSubject"=>"Please sign the enclosed envelope",
   "EmailBody"=>"Dear #RecipientFirstName# #RecipientLastName#\n\n#PersonalMessage#\n\nPlease sign the envelope #EnvelopeName#\n\nEnvelope will expire at #ExpirationDate#",
   "DisplayedEmailSender"=>"",
   "EnableReminders"=>true,
   "FirstReminderDayAmount"=>5,
   "RecurrentReminderDayAmount"=>3,
   "BeforeExpirationDayAmount"=>3,
   "DaysUntilExpire"=>28,
   "StatusUpdateCallbackUrl"=>"",
   "LockFormFieldsAtEnvelopeFinish"=>false,
   "Steps"=>
    [{"OrderIndex"=>1,
      "Recipients"=>
       [{"Email"=>"Placeholder:",
         "FirstName"=>"",
         "LastName"=>"",
         "LanguageCode"=>"",
         "DisableEmail"=>false,
         "AddAndroidAppLink"=>false,
         "AddIosAppLink"=>false,
         "AddWindowsAppLink"=>false,
         "AllowDelegation"=>true,
         "AllowAccessFinishedWorkstep"=>false,
         "SkipExternalDataValidation"=>false,
         "AuthenticationMethods"=>[],
         "IdentificationMethods"=>[]}],
      "RecipientType"=>"Signer"}]}}


怎么回事,我是不是误解了什么?我用的是Rails 7和Ruby 3。

[编辑]

expected_result

{
  'SspFileIds' => [
    '0823c9bf-d41e-4324-80be-3fb2c6ae49f0',
  ],
  'SendEnvelopeDescription' => {
    'CallbackUrl' => 'https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##',
    'WorkstepEventCallback' => {
      'Url' => 'https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##',
      'WhiteList' => [
        'string',
      ],
    },
    'Name' => '1 Recipient',
    'EmailSubject' => 'Please sign the enclosed envelope',
    'EmailBody' => "Dear #RecipientFirstName# #RecipientLastName#\n\n#PersonalMessage#\n\nPlease sign the envelope #EnvelopeName#\n\nEnvelope will expire at #ExpirationDate#",
    'DisplayedEmailSender' => '',
    'EnableReminders' => true,
    'FirstReminderDayAmount' => 5,
    'RecurrentReminderDayAmount' => 3,
    'BeforeExpirationDayAmount' => 3,
    'DaysUntilExpire' => 28,
    'StatusUpdateCallbackUrl' => '',
    'LockFormFieldsAtEnvelopeFinish' => false,
    'Steps' => [
      {
        'OrderIndex' => 1,
        'Recipients' => [
          {
            'Email' => 'teddy@olson.info',
            'FirstName' => 'Merle',
            'LastName' => 'Stracke',
            'LanguageCode' => 'en',
          },
        ],
        'RecipientType' => 'Signer',
      },
    ],
  },
}
vfh0ocws

vfh0ocws1#

您是否尝试过使用deep_mergehttps://apidock.com/rails/Hash/deep_merge
你想要的是test = first_hash.deep_merge(second_hash)

相关问题