【发布时间】: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
我的步骤和顾虑
- 创建的迁移和模型
- 建立模型关系
- 已创建控制器
- 配置种子文件和路由
- 运行 rails db:migrate, db:seed - 全部成功,没有错误持续
- 为所有 3 个模型添加了控制器操作
- 在 localhost:3000 中测试了路由,发现 powers json 数据未呈现
- 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