在 Rails 3 中创建一个字段 +来自数组/哈希的模型中的 mongo
假设我有一个包含这样的哈希的数组:
[{"head"=> {"title"=>"$20,000 Prize-Winning Chili",
"categories"=>[{"cat" => "Tex-mex"}]
它比这个大得多(它是一个完整的配方) - 但这应该足以证明我的问题。
在我的模型 Recipe.rb 中,我有:
def title
self["head"]["title"] rescue nil
end
这将在我的视图中提供recipe.title...但是,在我的控制器中,我希望能够执行以下操作:
@recipes = Recipe.where(title: "xyz")
但标题不是集合中的字段 - 所以它没有给我带来任何回报...这里有什么想法吗?我对 mongo 非常陌生 - 就像我昨天开始使用它一样。
谢谢!
So lets say I have an array containing a hash like this:
[{"head"=> {"title"=>"$20,000 Prize-Winning Chili",
"categories"=>[{"cat" => "Tex-mex"}]
Its much bigger than this (its a full recipe) - but this should be enough to demonstrate my question.
in my model Recipe.rb I have:
def title
self["head"]["title"] rescue nil
end
which will give me recipe.title in my views...however, in my controller, I want to be able to do things like:
@recipes = Recipe.where(title: "xyz")
But title isn't a field in the collection - so it returns nothing to me... any ideas here? I'm super new to mongo - as in I started with it yesterday.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这个:
Use this: