加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rake db:seed不起作用

发布时间:2020-12-17 02:23:00 所属栏目:百科 来源:网络整理
导读:我正在使用hstore进行项目,我有一个迁移文件: class CreateShippingCategories ActiveRecord::Migration def change create_table :shipping_categories do |t| t.string :name t.hstore :size t.integer :price_transport t.integer :price_storage t.time
我正在使用hstore进行项目,我有一个迁移文件:

class CreateShippingCategories < ActiveRecord::Migration
  def change
    create_table :shipping_categories do |t|
      t.string  :name
      t.hstore  :size
      t.integer :price_transport
      t.integer :price_storage
      t.timestamps
    end
  end
end

默认情况下我有4种类别,我决定在seeds.rb文件中创建它们,如下所示:

shipping_categories = [
  [ "Small",{L: 40,B: 30,H: 22},700,100],[ "Medium",{L: 60,B: 40,H: 32},900,400],[ "Large",B: 52,H: 140}],[ "XX Large",{L: 200,B: 50,H: 200}]
]

shipping_categories.each do |name,size,price_transport,price_storage|
  ShippingCategory.where(name: name,size: size,price_transport: price_transport,price_storage: price_storage).first_or_create
end

但是当我尝试运行rake db:seed时出现此错误:

rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "size"
LINE 1: ... WHERE "shipping_categories"."name" = 'Small' AND "size"."L"...
                                                         ^
: SELECT  "shipping_categories".* FROM "shipping_categories"  WHERE "shipping_categories"."name" = 'Small' AND "size"."L" = 40 AND "shipping_categories"."price_transport" = 700 AND "shipping_categories"."price_storage" = 100  ORDER BY "shipping_categories"."id" ASC LIMIT 1

有没有人有任何想法如何解决这个问题?

解决方法

问题是你在find_or_create的find部分查询的方式.我怀疑你要做的只是创建ShippingCategory,如果它不存在.这个名字是独特的吗?如果是这样你可以这样做:

ShippingCategory.
  create_with(name: name,price_storage: price_storage).
  find_or_create_by(name: name)

有关详细信息,请参阅find_or_create_by的文档.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读