简体   繁体   English

Rails 3.2如何将model.inspect转换为哈希?

[英]Rails 3.2 How do I convert model.inspect into a hash?

I'm capturing "point in time" (audit) data about certain model records using the inspect method to dump the state of the record to a string . 我正在使用inspect方法捕获有关某些模型记录的“时间点”(审核)数据,以将记录的状态转储到string For example after I've stored a User record in the variable a_user I call inspect and store the results in a string variable archived_user_data : 例如,在将User记录存储在变量a_user我调用inspect并将结果存储在字符串变量archived_user_data

1.9.3p484 :045 > archived_user_data = a_user.inspect
 => "#<User id: 17, email: \"ray.johnson@breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9>" 

1.9.3p484 :046 > archived_user_data
 => "#<User id: 17, email: \"ray.johnson@breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9>" 

When the archived_user_data is retrieved sometime in the future, I need to convert it into a hash. 当将来某个时间检索到archived_user_data ,我需要将其转换为哈希。 Is there a simple way to do this? 有没有简单的方法可以做到这一点? It looks like hashes converted to strings are usually converted back using eval , but in this case eval(archived_user_data) returns nil . 看起来转换为字符串的哈希通常是使用eval转换回来的,但是在这种情况下, eval(archived_user_data)返回nil

If you are still free to use Marshal, fine! 如果您仍然可以自由使用元帅,那就好! If not, I suggest you peel down the string to the hash part using 如果没有,我建议您使用以下方式将字符串剥离到哈希部分

s = archived_user_data.match(/#<User (.*)>/)[1]

after which you can reconstruct the hash using eval 之后,您可以使用eval重建哈希

eval("{" + s + "}")

Just do as below using attributes : 只需使用attributes执行以下操作:

Returns a hash of all the attributes with their names as keys and the values of the attributes as values. 返回与他们的名字作为和属性作为值的 属性哈希值

archived_user_data = a_user.attributes

You can use Marshal to dump and store any Ruby Object. 您可以使用元帅来转储和存储任何Ruby对象。

Example: 例:

(Using reference from @Arup's code) (使用@Arup的代码中的参考)

data_hash = a_user.attributes
dump_string = Marshal.dump(data_hash)
retrieved_hash = Marshal.load(dump_string)

You can store dump_string in file or database or in any other storage area. 您可以将dump_string存储在文件或数据库或任何其他存储区域中。

EDIT 编辑

Specific case: 具体情况:

2.1.0 :013 > {:a => "b"}.inspect
 => "{:a=>\"b\"}"
2.1.0 :014 > "{id: 17, email: \"ray.johnson@breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}"
 => "{id: 17, email: \"ray.johnson@breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}"
2.1.0 :015 > eval("{id: 17, email: \"ray.johnson@breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}")
 => {:id=>17, :email=>"ray.johnson@breakfs.com", :encrypted_password=>"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....", :created_at=>"2014-04-05 21:42:09", :updated_at=>"2014-04-05 21:43:25", :account_id=>9}

You need to understand that hashes when inspected and stored as string aren't of the form: 您需要了解,在检查并存储为字符串时,哈希不是以下形式:

"#<User id: 17, email: \"ray.johnson@breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9>"

But should be of the form: 但应采用以下形式:

"{id: 17, email: \"ray.johnson@breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}"

You can eval and get back the hash if you modify your string to the format above. 如果将字符串修改为上述格式,则可以评估并获取哈希值。 Refer to my three line example above. 请参阅上面的三行示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM