2

我有一个具有多个属性( 、 等...)的 Products 模型,customer_typecustomer_name正在dept_type尝试用一些数据为其播种。

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

 Product.create(customer_type:'Retailer', customer_name: 'Walmart', dept_type: 'Grocery')

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

我也尝试过rake db:setuprake db:reset每次它都没有返回错误消息,但没有加载数据。

更新我修改了我的数据库种子文件看起来像这样

  Product.create!(customer_type:'Retailer', customer_name: 'Walmart', dept_type: 'Grocery')

当我运行 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

我已经尝试使用客户类型值零售商和制造商来播种数据库,但运气不好

4

2 回答 2

8

您的模型可能无法保存。您忽略了create通话中的任何错误。改为使用Product.create!,如果您的创建失败,它将引发异常。

于 2014-02-25T01:34:56.473 回答
0

我认为问题出在您的语法上。请试试

validates : customer_type, :inclusion => { :in => CUSTOMER_TYPES }它会正常工作。

如果我是你,我会使用enums这种类型的场景。使用Enums完美绑定列。

于 2019-04-22T15:51:54.893 回答