NameError:seed.rb上的未初始化常量

时间:2017-01-06 21:05:46

标签: ruby-on-rails ruby

在为我的应用程序执行seed.rb时,我在运行rake db:seed时遇到以下错误:

rake aborted!
NameError: uninitialized constant Menu::CourseMenu
C:in `destroy_all'
C:.../db/seeds.rb:11:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

菜单:

class Menu < ActiveRecord::Base
  belongs_to :user
  has_many :course_menus, dependent: :destroy
end

CoursesMenu:

class CoursesMenu < ActiveRecord::Base
  belongs_to :menu
  has_and_belongs_to_many :recipes, dependent: :destroy
end

Seed.rb:

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#   Mayor.create(name: 'Emanuel', city: cities.first)


User.destroy_all
Menu.destroy_all
CookBook.destroy_all
CoursesMenu.destroy_all
Recipe.destroy_all



User.create! [
  { email: "jean@mail.com", password: "12345", status: true },
  { email: "wendy@mail.com", password: "12345", status: true  },
  { email: "jose@mail.com", password: "12345", status: true  },
  { email: "vivi@mail.com", password: "12345", status: true  },
  { email: "emilio@mail.com", password: "12345", status: true  }
]

sample_menus = Menu.create! [
  { name: "For Mom", description: "Menu to lose weight" },
  { name: "For Dad", description: "Menu to gain more muscle" },
  { name: "For the lil tykes", description: "Menu so they stop being fat" },
  { name: "For the Vegan Aunt", description: "For the old harpy" }
]

sample_cookbooks = CookBook.create! [
  { name: "Jean'selections", description: "Premiun choices by Jean" },
  { name: "Wendy'selections", description: "Premiun choices by Wendy" },
  { name: "Jose'selections", description: "Premiun choices by Jose" },
  { name: "Best of All", description: "JPremiun choices by the Crew" }
]

sample_recipes = Recipe.create! [
  { img_url: "http://static.food2fork.com/BrownieFeature193f.jpg", title: "Slutty Brownies", source_url: "http://whatsgabycooking.com/slutty-brownies/"},
  { img_url: "http://static.food2fork.com/pizza292x2007a259a79.jpg", title: "Homemade Pizza", source_url: "http://www.simplyrecipes.com/recipes/homemade_pizza/" },
  { img_url: "http://static.food2fork.com/5337400468_d5892f3a03_od5cd.jpg", title: "Chicken Tortilla Soup", source_url: "http://thepioneerwoman.com/cooking/2011/01/chicken-tortilla-soup/"},
  { img_url: "http://static.food2fork.com/GuacamoleGrilledCheese6019.jpg", title: "Guacamole Grilled Cheese Sandwich", source_url: "http://www.twopeasandtheirpod.com/guacamole-grilled-cheese-sandwich/"}
]


sample_courses_menus = CoursesMenu.create! [

  { course_name: "Breakfast", day: "Monday" },
  { course_name: "Day Snack", day: "Monday" },
  { course_name: "Lunch", day: "Monday" },
  { course_name: "Dinner", day: "Monday" },
  { course_name: "Night Snack", day: "Monday" },

  { course_name: "Breakfast", day: "Tuesday" },
  { course_name: "Day Snack", day: "Tuesday" },
  { course_name: "Lunch", day: "Tuesday" },
  { course_name: "Dinner", day: "Tuesday" },
  { course_name: "Night Snack", day: "Tuesday" },

  { course_name: "Breakfast", day: "Wednesday" },
  { course_name: "Day Snack", day: "Wednesday" },
  { course_name: "Lunch", day: "Wednesday" },
  { course_name: "Dinner", day: "Wednesday" },
  { course_name: "Night Snack", day: "Wednesday" },

  { course_name: "Breakfast", day: "Thursday" },
  { course_name: "Day Snack", day: "Thursday" },
  { course_name: "Lunch", day: "Thursday" },
  { course_name: "Dinner", day: "Thursday" },
  { course_name: "Night Snack", day: "Thursday" },

  { course_name: "Breakfast", day: "Friday " },
  { course_name: "Day Snack", day: "Friday " },
  { course_name: "Lunch", day: "Friday " },
  { course_name: "Dinner", day: "Friday " },
  { course_name: "Night Snack", day: "Friday " },

  { course_name: "Breakfast", day: "Saturday" },
  { course_name: "Day Snack", day: "Saturday" },
  { course_name: "Lunch", day: "Saturday" },
  { course_name: "Dinner", day: "Saturday" },
  { course_name: "Night Snack", day: "Saturday" },

  { course_name: "Breakfast", day: "Sunday" },
  { course_name: "Day Snack", day: "Sunday" },
  { course_name: "Lunch", day: "Sunday" },
  { course_name: "Dinner", day: "Sunday" },
  { course_name: "Night Snack", day: "Sunday" }
]


sample_cookbooks. << 20.times { |index| CookBook.create! name: "CookBook#{index}", description: "This is a cookbook sample #{index}" }



CoursesMenu.all.each do |course_menu|
  course_menu.recipes = sample_recipes.sample
  course_menu.save!
end

Menu.all.each do |menu|
  menu.course_menus = sample_courses_menus.sample
  menu.save!
end

CookBook.all.each do |book|
  book.recipes = sample_recipes.sample
  book.save!
end

User.all.each do |user|
  user.cook_books = sample_cookbooks.sample
  user.menus = sample_menus.sample
  user.save!
end

我怀疑我处理错误的关系是因为如果我在seed.rb中将destroy_all更改为delete_all,则此特定错误不会发生。

我在跳跃,有人可以指出我正确的方向来解决这个问题。

2 个答案:

答案 0 :(得分:2)

问题

Menu有很多:course_menus

但你的班级是

CoursesMenu

解决方案

您可以将CoursesMenu重命名为CourseMenu可以有多个:courses

无论您选择什么,都可以检查名称是否正确:

:courses.to_s.capitalize.singularize
#=> "Course"
:course_menus.to_s.camelcase.singularize
#=> "CourseMenu"

答案 1 :(得分:2)

Menu模型中,更改

has_many :course_menus, dependent: :destroy

has_many :courses_menus, dependent: :destroy

CoursesMenu定义为CourseMenu会更正确吗?如果是这样,你的关联将是正确的。

此外,FYI,destroy_all失败和delete_all失败的原因是因为destroy_all单独删除每条记录,在此过程中执行回调(包括dependent: :destroy s)。这是发生错误的地方,因为Rails尝试使用类型CourseMenu的模型,这种模型不像您定义类的方式那样存在。相比之下,delete_all只是在一个查询(DELETE from menus)中删除有问题的记录,并且不会尝试执行任何回调代码,因此Rails从不会遇到错误命名的资源。