【问题标题】:rake db:seed NameError: uninitialized constant Exercice in rails consolerake db:seed NameError:rails 控制台中未初始化的常量 Exercice
【发布时间】: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


【解决方案1】:

ExerciceExercise 类不匹配。

models.items << Exercice.create(name: "The Active Record pattern", content: "Exo about active record pattern")

class Exercise < ActiveRecord::Base 
  acts_as :item 
end

【讨论】:

  • 它有效!太棒了。我想摧毁种子。你知道怎么做吗?
  • 如果回答正确,请采纳。我不确定销毁种子是什么意思,但是您可以通过在控制台中键入rake db:drop 然后rake db:migrate 来重置数据库(假设您使用的是 sqlite/postgres)
  • 是的,但我只想在我的 show#view 上添加一个按钮删除或销毁它的方法。我想我的问题不清楚,抱歉。
猜你喜欢
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多