【问题标题】:Rails NoMethodError: undefined method `name' for "#<RecipeType:0x000055cd000b18a0>":StringRails NoMethodError:“#<RecipeType:0x000055cd000b18a0>”的未定义方法“名称”:字符串
【发布时间】:2020-03-26 01:46:07
【问题描述】:

我在挑战 Active Records 时遇到了麻烦,我阅读了文档,并看到了我重新制作和工作的 belongs_to 的其他示例,我不知道我在这里做错了什么我尝试调用 recipe.recipe_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 < ActiveRecord::Migration[5.2]
  def change
    add_column :recipes, :ingredients, :text
    add_column :recipes, :cook_method, :text
  end
end
class CreateRecipeTypes < ActiveRecord::Migration[5.2]
  def change
    create_table :recipe_types do |t|
      t.string :name

      t.timestamps
    end
  end
end
class AddRecipeRefToRecipeType < ActiveRecord::Migration[5.2]
  def change
    add_reference :recipe_types, :recipe_type, foreign_key: true
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby rails-activerecord


    【解决方案1】:

    您似乎将recipe_type 引用添加到了错误的表中。您的最后一次迁移应该是

    add_reference :recipes, :recipe_type, foreign_key: true
    

    因为您已将reference_type 引用添加到ReferenceType。

    【讨论】:

    • 谢谢,我没有注意到,回滚并更改了它,但现在我收到了NameError (undefined local variable or method recipe_type'`
    • 你是否在你的Recipe模型中指定了belongs_to :recipe_type
    • 好吧,也许你正试图在某个地方调用 recipe_type,而实际上你应该调用 recipe。搜索 recipe_type 并尝试找出不正确的用法。如果您仍然可以让它工作,请编辑您的问题并包含完整的错误回调日志
    【解决方案2】:

    所以最终的架构是:

    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
    

    模型/recipe_type

    class RecipeType < ApplicationRecord
        has_many :recipes
    end
    

    模型/配方

    class Recipe < ApplicationRecord
      belongs_to :recipe_type
    end
    

    你可以开始一个简单的 has_many belongs_to 活动记录关联,我希望这能帮助和帮助我的人一样多,在这个旅程的开始,强烈建议学习 db:migrate, db:rollback, db:create 和db:drop 给那些遇到麻烦的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2013-12-25
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多