验证错误播种Rails 3 w Seeds.rb

Ren*_*ade 2 ruby-on-rails seeding

我有几个属性(一个产品模型customer_type,customer_name,dept_type等...),我想用一些数据种子吧.

我的db/seeds.rb文件中有以下内容

 Product.create(customer_type:'Retailer', customer_name: 'Walmart', dept_type: 'Grocery')
Run Code Online (Sandbox Code Playgroud)

我保存文件然后运行rake db:Seed我得到没有错误消息,但当我加载我的应用程序时,没有数据存在?我在这做错了什么?

我也试过rake db:setup,rake db:reset每次都没有返回错误信息,但数据没有加载.

更新我修改了我的db种子文件,看起来像这样

  Product.create!(customer_type:'Retailer', customer_name: 'Walmart', dept_type: 'Grocery')
Run Code Online (Sandbox Code Playgroud)

当我运行rake db:reset我得到错误"验证失败:客户类型不包含在列表中"

我的产品模型文件带有验证

 class Product < ActiveRecord::Base
   attr_accessible  :customer_type

   has_many :line_items
   has_many :orders, through: :line_items

   CUSTOMER_TYPES = ["Retailer", "Manufacturer"]
   validates :customer_type, inclusion: CUSTOMER_TYPES
Run Code Online (Sandbox Code Playgroud)

我尝试使用客户类型值Retailer和制造商没有运气来播种数据库

mea*_*gar 7

您的模型很可能无法保存.你忽略了create通话中的任何错误.Product.create!相反,请使用,如果您的创建失败,则会引发异常.