3

私はかなり基本的な問題を抱えていますが、正しく動作させることができないようです。

これがセットアップです-

class Recipe < ActiveRecord::Base
 has_many :recipeIngredients
 has_many :ingredients :through => :recipeIngredients
end

class Ingredient < ActiveRecord::Base
 has_many :recipeIngredients
 has_many :recipes :through => :recipeIngredients
end

class RecipeIngredients < ActiveRecord::Base
 belongs_to :recipe
 belongs_to :ingredients
end

各材料には ID と名前があり、レシピには ID とタイトルがあり、RecipeIngredients にはレシピ ID、材料 ID、量があります。

を使用してレンダリングしようとすると

@recipe = Recipe.find(params[:id])
render :json => @recipe, :include => :ingredients

材料を取得しましたが、RecipeIngredients から量や名前にアクセスできません。- これは出力します

{
    "list_items": {
        "id": 1,
        "title": "Foo",
        "description": null,
        "ingredients": [
            {
                "id": 1
            },
            {
                "id": 2
            },
            {
                "id": 3
            },
            {
                "id": 4
            }
        ]
    }
}

:ingredients を呼び出すときに次のような結果が得られるように、どうすれば材料とレシピの材料の関係を作成できますか?

{
 "id":1,
 "name":"foo",
 "amount":"2 oz"
}

ありがとう!

4

1 に答える 1