获取 MongoMapper 模型的键
假设有以下模型:
class Article
include MongoMapper::Document
key :name, String
key :body, String
end
我可以将模型的键放入数组或哈希中吗?
Suppose the following model:
class Article
include MongoMapper::Document
key :name, String
key :body, String
end
Could I get the keys of the model into an array or hash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Article.keys
将为您提供关键对象的关键名称的哈希值。如果您只需要键名称,请尝试Article.keys.keys
。但请注意,如果您动态设置模型中未声明的任何键,或者加载具有模型中不存在的额外键的对象,它们将显示在键哈希中。请参阅https://github.com/jnunemaker/mongomapper/issues/195。
Article.keys
will give you a hash of key names to key objects. If you just want the key names, tryArticle.keys.keys
.Watch out though, if you dynamically set any keys that aren't declared in your model, or you load an object that has extra keys that aren't in your model, they will show up in the keys hash. See https://github.com/jnunemaker/mongomapper/issues/195.