Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails NoMethodError:未定义的方法`name';至于#&书信电报;RecipeType:0x000055cd000b18a0>&引用;:一串_Ruby On Rails_Ruby_Rails Activerecord - Fatal编程技术网

Ruby on rails Rails NoMethodError:未定义的方法`name';至于#&书信电报;RecipeType:0x000055cd000b18a0>&引用;:一串

Ruby on rails Rails NoMethodError:未定义的方法`name';至于#&书信电报;RecipeType:0x000055cd000b18a0>&引用;:一串,ruby-on-rails,ruby,rails-activerecord,Ruby On Rails,Ruby,Rails Activerecord,我一直很难对活动记录进行挑战,我阅读了文档,看到了我重新制作和工作的属于的其他示例,当我尝试调用recipe.recipe\u type.name时,我再也不知道我在这里做错了什么。我得到了错误Rails NoMethodError:undefined method`name',for“#”:String schema.rb create_table "recipe_types", force: :cascade do |t| t.string "name" t.datet

我一直很难对活动记录进行挑战,我阅读了文档,看到了我重新制作和工作的
属于
的其他示例,当我尝试调用recipe.recipe\u type.name时,我再也不知道我在这里做错了什么。我得到了错误Rails NoMethodError:undefined method`name',for“#”:String

schema.rb


  create_table "recipe_types", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "recipe_type_id"
    t.index ["recipe_type_id"], name: "index_recipe_types_on_recipe_type_id"
  end

  create_table "recipes", force: :cascade do |t|
    t.string "title"
    t.string "cuisine"
    t.string "difficulty"
    t.integer "cook_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "ingredients"
    t.text "cook_method"
  end
end
迁移

  def change
    create_table :recipes do |t|
      t.string :title
      t.string :cuisine
      t.string :difficulty
      t.integer :cook_time

      t.timestamps
    end
  end
end
class AddFieldsToRecipe
class CreateRecipeTypes
class AddRecipeRefToRecipeType
您似乎将
配方类型
引用添加到了错误的表中。您的上一次迁移可能是

add_reference :recipes, :recipe_type, foreign_key: true

因为实际上,您已经将reference类型reference添加到ReferenceType。

因此最终的模式是:

ActiveRecord::Schema.define(version: 2020_03_26_013134) do

  create_table "recipe_types", force: :cascade do |t|
    t.string "name"
    t.integer "recipe_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["recipe_id"], name: "index_recipe_types_on_recipe_id"
  end

  create_table "recipes", force: :cascade do |t|
    t.string "title"
    t.string "cuisine"
    t.string "difficulty"
    t.integer "cook_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "ingredients"
    t.text "cook_method"
    t.integer "recipe_type_id"
    t.index ["recipe_type_id"], name: "index_recipes_on_recipe_type_id"
  end
end

型号/配方类型

class RecipeType < ApplicationRecord
    has_many :recipes
end
类RecipeType
模型/配方

class Recipe < ApplicationRecord
  belongs_to :recipe_type
end
类配方

你可以开始一个简单的has\u many allows\u to active record association,我希望这能帮助和我一样多的人,在这段旅程的开始,强烈推荐学习db:migrate、db:rollback、db:create和db:drop,以帮助那些遇到麻烦的人

谢谢,我没有注意到,我做了一个回滚并更改了它,但是现在我得到了
namererror(未定义的局部变量或方法
recipe\u type'`您是否在配方模型中指定了
属于:recipe\u type
?好的,也许您正试图在某个地方调用recipe\u type,而实际上您应该调用recipe。搜索recipe\u type并尝试查找不正确的用法。如果您仍然可以让它工作,请编辑您的问题并单击此处。)包括完整的错误回调日志
ActiveRecord::Schema.define(version: 2020_03_26_013134) do

  create_table "recipe_types", force: :cascade do |t|
    t.string "name"
    t.integer "recipe_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["recipe_id"], name: "index_recipe_types_on_recipe_id"
  end

  create_table "recipes", force: :cascade do |t|
    t.string "title"
    t.string "cuisine"
    t.string "difficulty"
    t.integer "cook_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "ingredients"
    t.text "cook_method"
    t.integer "recipe_type_id"
    t.index ["recipe_type_id"], name: "index_recipes_on_recipe_type_id"
  end
end
class RecipeType < ApplicationRecord
    has_many :recipes
end
class Recipe < ApplicationRecord
  belongs_to :recipe_type
end