【发布时间】:2016-04-24 05:03:50
【问题描述】:
我正在开发一个课程应用程序来学习语言。我迁移了许多模型作为课程、章节和项目(使用acts_as-active_record gem)。项目是actable,课程、练习和考试是act_as项目。 我创建了这样的种子:
rails = Course.create(name: "Ruby On Rails")
models = rails.chapters.create(name: "Models")
# first item is a lesson
models.items << Lesson.create(name: "What is Active Record?", content: "Lesson content here")
# then 2 exos
models.items << Exercise.create(name: "The Active Record pattern", content: "Exo about active record pattern")
models.items << Exercise.create(name: "Object Relational Mapping", content: "Exo about ORM")
models.items << Exercise.create(name: "Active Record as an ORM Framework", content: "Exo about ORM")
# a second lesson
models.items << Lesson.create(name: "Convention over Configuration in Active Record", content: "Lesson content here")
# 3 exos
models.items << Exercise.create(name: "Naming Conventions", content: "Exo about naming convention")
models.items << Exercise.create(name: "Schema Conventions", content: "Exo about schema convention")
# a summary lesson
models.items << Lesson.create(name: "Model summary", content: "Lesson content here")
# an exam
models.items << Exam.create(name: "Rails Models exam", content: "Exam content here")
# You can go to next course with : next_item = Course.first.chapters.first.items.first.lower_item
# Then go to next chapter with: next_item.chapter.lower_item
但是在我的 rails 控制台 rake db:seed 我有一个错误通知
耙中止!
NameError: 未初始化的常量 Exercice
../db/seeds.rb:19:in <top (required)>'
-e:1:in'
任务:TOP => db:seed
我真的不知道错误是什么。 也许与acts_as-active_record有关系吗?
以下是模型:
class Course < ActiveRecord::Base
has_many :chapters, -> { order(position: :asc) }
validates_presence_of :title
end
class Chapter < ActiveRecord::Base
acts_as_list scope: :course
belongs_to :course
has_many :items, -> { order(position: :asc) }
validates_presence_of :title
end
class Item < ActiveRecord::Base
actable
belongs_to :chapter
acts_as_list scope: :chapter
validates_presence_of :title
end
class Lesson < ActiveRecord::Base
acts_as :item
end
class Exercise < ActiveRecord::Base
acts_as :item
end
class Exam < ActiveRecord::Base
acts_as :item
end
整个种子都在这里。
【问题讨论】:
-
你能把你的模型声明和关系放进去吗?
-
这可能无关,但在您的示例中,Exercise 拼写错误。
-
你能粘贴整个
db/seeds.rb文件吗? -
整个db/seeds.rb在上面。
-
请看上面@pjam
标签: ruby-on-rails ruby