0

私はレールが初めてで、解決できない問題があります。

私はモデルレシピを持っています

  class Recipe < ActiveRecord::Base
    has_many :items, :dependent => :destroy
    accepts_nested_attributes_for :items,**

とモデルアイテム

  class Item < ActiveRecord::Base
    belongs_to :recipe
  end

そして、アイテム属性へのアクセスに問題がありますRecipe.Example

@i文字列の説明フィールドを持つアイテム ( ) が"test_"あり、id = 1

そして、説明付きの Recipe( @r) があります。id=2"test_recipe"

を使用してアイテムをレシピに正しく関連付けることができます

@i.recipe_id = 2

もしそうなら@i、私は結果を持っています

#<Recipe id: 2, description: "test_recipe", created_at: "2012-04-14 15:11:00", updated_at: "2012-04-14 15:11:00"`

しかし、もしそうなら@r.items、私は結果を持っています

Item id: 1,recipe_id: 2, updated_at: "2012-04-14 15:11:00" , description: nil)

彼はアイテムの説明フィールドにアクセスできません。なんで?そして、これは、レールが項目フィールドを構築しないため、レシピの適切なフォームを構築することを避けています.

4

1 に答える 1

0

これは、次のようなことをしようとしているためだと思います。

@recipe.items.description

各項目の説明だけが必要な場合:

@recipe.items.map(&:description)
于 2012-04-16T14:35:45.863 に答える