[英]mapping to value in deep hash
I'm doing an application using mongodb and mongoid and I'm facing a problem where I need to map something in one document to something in another document. 我正在使用mongodb和mongoid进行应用程序处理,并且遇到了一个问题,我需要将一个文档中的内容映射到另一个文档中的内容。 My plan is to store something in a document that I can then use to figure out what value for it to fetch from a different collection.
我的计划是将某些东西存储在文档中,然后可以用来确定从另一个集合中获取它的价值。 But, this is more generally a ruby question about how I can fetch data from deep within a hash.
但是,这更笼统地是一个关于如何从哈希表的深层获取数据的红宝石问题。
I have a structure something like this: 我有这样的结构:
Widget
Sections
0
Fields
0
value: foobar
If that makes sense. 如果这样的话。 Let's say I want to get the value of the first field in the first section, I would do something like:
假设我想获得第一部分中第一个字段的值,我将执行以下操作:
@widget.sections[0].fields[0].value
No problem. 没问题。
Now the question is, how can I do this with all of that as a string? 现在的问题是,我怎样才能将所有这些都当作字符串呢? What I want to do is store within the database a mapping value.
我要做的是在数据库中存储一个映射值。 So I've have a key/value with something like:
所以我有一个类似的键/值:
mapping: "sections[0].fields[0].value"
Now how can I use that to get the data from @widget? 现在如何使用它从@widget获取数据? I've tried @widget.send "sections[0].fields[0].value" but that does not work... I can do @widget.send "sections" and get back an array of sections, but I'm not quite sure how to take it further...
我已经尝试过@ widget.send“ sections [0] .fields [0] .value”,但是那行不通...我可以做@ widget.send“ sections”并取回一组节,但是我我不太确定如何使它更进一步...
So to summarize, I can do this: 综上所述,我可以这样做:
@widget.sections[0].fields[0].value
if I have @widget
and a string "sections[0].fields[0].value" how can I execute that? 如果我有
@widget
和字符串“ sections [0] .fields [0] .value”,该如何执行?
@widget.instance_eval("sections[0].fields[0].value")
应该可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.