Я новичок в RoR и прошу прощения, если я не express себя ясно. И извините за неудобства, вызванные размещением кодов вместо картинок (недостаточно репутации qwq). Схема моей базы данных выглядит следующим образом:
ActiveRecord::Schema.define(version: 20200323173103) do
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "uid"
t.integer "student_id"
end
add_index "posts", ["student_id"], name: "index_posts_on_student_id"
create_table "users", force: :cascade do |t|
t.string "name"
t.integer "year"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "uid"
t.integer "student_id"
end
add_index "users", ["student_id"], name: "index_users_on_student_id"
create_table "students", force: :cascade do |t|
t.string "name"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
И мой файл seed.rb:
Student.destroy_all
Post.destroy_all
User.destroy_all
Post.create!([
{ title: "hello world", content: "first web app." },
{ title: "Severus Snape", content: "Always." },
{ title: "Albus Dumbledore", content: "'I am not worried, Harry,' said Dumbledore, his voice a little stronger despite the freezing water.' I am with you.'" },
{ title: "Albus Dumbledore", content: "“But you know, happiness can be found even in the darkest of times, if one only remembers to turn on the light." },
{ title: "Hermione Granger", content: "'I hope you are pleased with yourselves. We could all have been killed — or worse, expelled. Now if you don't mind, I'm going to bed.'" },
{ title: "Albus Dumbledore", content: "It does not do well to dwell on dreams and forget to live." },
{ title: "Ron Weasley", content: "You're a little scary sometimes, you know that? Brilliant ... but scary." },
{ title: "Rubeus Hagrid", content: "Yer a wizard Harry." },
{ title: "Dobby", content: "Dobby is free." },
{ title: "Gilderoy Lockhart", content: "Fame is a fickle friend, Harry. Celebrity is as celebrity does. Remember that." },
{ title: "Harry Potter", content: "I solemnly swear I am up to no good." }
])
users = User.create!([
{ uid:1, name: "zyj",year: 2000 },
{ uid:2, name: "jz zhou",year: 1995 },
{ uid:3, name: "mitty",year: 2002 },
{ uid:4, name: "stela",year: 1985 },
{ uid:5, name: "someone",year: 1993 },
{ uid:6, name: "you know who",year: 1863}
])
students = Student.create! ([
{ name: "Joe", password: "abc123" },
{ name: "Jim", password: "123abc" },
{ name: "zyj", password: "0303267337" },
{ name: "Joseph", password: "123456" }
])
Post.all.each do |p|
p.uid = users.sample.uid
p.save!
end
User.all.each do |u|
@sid = students.sample.id
u.student_id = @sid
Post.find_by(uid: u.uid).each do |po|
po.student_id = @sid
po.save!
end
u.save!
end
В то время как он выдает сообщение об ошибке ниже, когда я запускаю "rake db: seed":
jhzhengtekiMacBook-Air:my_blog severina$ rake db:seed
rake aborted!
NoMethodError: undefined method `each' for #<Post:0x00007fe59cdd8bd0>
/Users/severina/Desktop/my_blog/db/seeds.rb:52:in `block in <top (required)>'
/Users/severina/Desktop/my_blog/db/seeds.rb:49:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Так что же произошло в строках 49 и 52? Спасибо за помощь !!!!