在rails中插入属性到hash

时间:2013-10-30 11:23:49

标签: ruby-on-rails ruby hash ruby-on-rails-4

我正在生成包含如下消息属性的哈希“消息”:

  message = Message.create_from_mail_message(mail, @current_sender_email, expires_at, public, message_type)

现在我想为此哈希“消息”添加一个额外的属性“access_key”问题是此属性不在消息模型中但在收据中。任何关于如何做到这一点的想法。

现在消息哈希给我回复:

{"active":1,"body":"","created_at":"2013-10-30T10:49:41Z","creator_id":6,"expires_at":"2013-11-29T10:49:41Z","id":25,"message_type_id":1,"public":0,"reply_to":null,"slug":"fa2fd66f-7e32-4e3f-898b-8412c676a0ff","subject":"2af03892533ffb43\ufffdh\ufffdf\ufffdS+9<\b&\u0017\ufffd\u0016/\ufffd","updated_at":"2013-10-30T10:49:41Z"}

“access_key”可以通过以下方式检索:

     access_key = Reciept.access_key

提前致谢。

4 个答案:

答案 0 :(得分:6)

您应该将access_key与消息哈希中的值合并,如:

message.merge!(“access_key”=&gt; Reciept.access_key)

答案 1 :(得分:4)

试试这个:

message.merge!('access_key' => Reciept.access_key)

答案 2 :(得分:1)

message.merge!{access_key: Reciept.access_key}

答案 3 :(得分:0)

Standar说:

message.merge(:access_key=>Reciept.access_key)

我希望能帮到你。