【问题标题】:Rails db: seed issue. I am receiving a strange error regarding the seed fileRails db:种子问题。我收到一个关于种子文件的奇怪错误
【发布时间】:2021-12-14 22:39:39
【问题描述】:

错误

????‍♀️ Seeding powers...
????‍♀️ Seeding heroes...
????‍♀️ Adding powers to heroes...
rails aborted!
ActiveRecord::RecordNotFound: Couldn't find Power without an ID
/Users/nd/Desktop/code-practice/db/seeds.rb:33:in `block (2 levels) in <top (required)>'
/Users/nd/Desktop/code-practice/db/seeds.rb:31:in `times'
/Users/nd/Desktop/code-practice/db/seeds.rb:31:in `block in <top (required)>'
/Users/nd/Desktop/code-practice/db/seeds.rb:30:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

种子文件

Hero.destroy_all 
HeroPower.destroy_all

puts "????‍♀️ Seeding powers..."
Power.create([
  { name: "super strength", description: "gives the wielder super-human strengths" },
  { name: "flight", description: "gives the wielder the ability to fly through the skies at supersonic speed" },
  { name: "super human senses", description: "allows the wielder to use her senses at a super-human level" },
  { name: "elasticity", description: "can stretch the human body to extreme lengths" }
])

puts "????‍♀️ Seeding heroes..."
Hero.create([
  { name: "Kamala Khan", super_name: "Ms. Marvel" },
  { name: "Doreen Green", super_name: "Squirrel Girl" },
  { name: "Gwen Stacy", super_name: "Spider-Gwen" },
  { name: "Janet Van Dyne", super_name: "The Wasp" },
  { name: "Wanda Maximoff", super_name: "Scarlet Witch" },
  { name: "Carol Danvers", super_name: "Captain Marvel" },
  { name: "Jean Grey", super_name: "Dark Phoenix" },
  { name: "Ororo Munroe", super_name: "Storm" },
  { name: "Kitty Pryde", super_name: "Shadowcat" },
  { name: "Elektra Natchios", super_name: "Elektra" }
])

puts "????‍♀️ Adding powers to heroes..."

strengths = ["Strong", "Weak", "Average"]
Hero.all.each do |hero|
  rand(1..3).times do
    # get a random power
    power = Power.find(Power.pluck(:id).sample)

    HeroPower.create!(hero_id: Hero.first.id, power_id: Power.first.id, strength: strengths.sample)
  end
end

puts "????‍♀️ creating hero powers for heroes..."
HeroPower.create!(hero_id: Hero.all.sample, power_id: Power.all.sample, strength: strengths.all.sample)
# HeroPower.create!(hero_id: , power_id: Power.first.id, strength: strengths.sample)

puts "????‍♀️ Done seeding!"

架构文件

ActiveRecord::Schema.define(version: 2021_10_22_025009) do

  create_table "hero_powers", force: :cascade do |t|
    t.string "strength"
    t.integer "hero_id"
    t.integer "power_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "heroes", force: :cascade do |t|
    t.string "name"
    t.string "super_name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "powers", force: :cascade do |t|
    t.string "name"
    t.string "description"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end

功率模型

class Power < ApplicationRecord
    has_many :hero_powers
    has_many :heroes, through: :hero_powers

    validates :description, presence: true, length: { maximum: 20 }
end

我的步骤和顾虑

  1. 创建的迁移和模型
  2. 建立模型关系
  3. 已创建控制器
  4. 配置种子文件和路由
  5. 运行 rails db:migrate, db:seed - 全部成功,没有错误持续
  6. 为所有 3 个模型添加了控制器操作
  7. 在 localhost:3000 中测试了路由,发现 powers json 数据未呈现
  8. rails c > Power.all

结果

irb(main):001:0> Power.all
   (2.2ms)  SELECT sqlite_version(*)
  Power Load (1.0ms)  SELECT "powers".* FROM "powers" /* loading for inspect */ LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation []>

此时我明白ActiveRecord::RecordNotFound: Couldn't find Power without an ID 很遗憾我不知道如何解决这个问题。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    能否添加 Power 模型文件,可能存在未通过的验证规则。比如最大/最小长度。

    class Person < ApplicationRecord
      validates :name, length: { minimum: 2 }
      validates :bio, length: { maximum: 500 }
      validates :password, length: { in: 6..20 }
      validates :registration_number, length: { is: 6 }
    end
    

    您可以在rails console 调试这些方法,使用rails 控制台可以查看日志。

    使用 Power 模型验证,您的 Power 模型实例描述似乎有超过 20 个字符串长度。您可以从 validates :description、presence: true、length: { maximum: 20 } 中删除 length: { maximum: 20 } 或提高最大值的限制,例如 length: { maximum: 250 },或者您可以在您的种子数据。

    例如 validates :description, presence: true, length: { maximum: 250 }

    【讨论】:

    • 我有一个 Power 模型。贴在上面。
    • 我更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 2020-12-12
    • 1970-01-01
    • 2021-11-29
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多