简体   繁体   中英

create a field in rails 3 + mongo in a model from an array / hash

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!

用这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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