在 Rails 3 中创建一个字段 +来自数组/哈希的模型中的 mongo

发布于 2024-12-23 12:10:10 字数 529 浏览 2 评论 0原文

假设我有一个包含这样的哈希的数组:

[{"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-12-30 12:10:10

使用这个:

@recipes = Recipe.where('head.title' => "xyz")

Use this:

@recipes = Recipe.where('head.title' => "xyz")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文