Elliot
Elliot

Reputation: 13845

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!

Upvotes: 0

Views: 133

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230551

Use this:

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

Upvotes: 1

Related Questions