簡體   English   中英

如何 append 一個 hash 到另一個 hash 這些哈希值是一個鍵的值?

[英]How to append a hash to another hash where these hashes are value of a key?

最終的 output 在 postgres 列名extras的 jsonb 列中如下所示

{“notes”: {“timestamp”: 1620742796, “prev_notes”: “someTest”, “new_notes”: “someText”},
        {“timestamp”: 1620742652, “prev_notes”: “someText”, “new_notes”: “AllGood”}
}

以下是 DB 中的初始 state

{“notes”: {“timestamp”: 1620742796, “prev_notes”: “someTest”, “new_notes”: “someText”}

在 Rails 控制台上

stud = Student
stud.extras["notes"] = <gives current  state>

在我的 ruby on rails 代碼中,我構建了一個 obj

obj = {“timestamp”: 1620742652, “prev_notes”: “someText”, “new_notes”: “AllGood”}

現在我想把 obj 上面的 append 改為鍵“notes”的值。 我試過 append,推,反向合並,。 <<。 沒有任何效果。

extra[:notes].merge(obj)

您可以這樣做將其合並到一個 hash 中,或者您可以將注釋轉換為數組,然后保存一組哈希值。

這不是一個有效的語法:

{notes: {}, {}}

但這是有效的:

{notes: [{}, {}]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM