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

ruby-on-rails – rake db:seed不起作用,第1张

概述我正在使用hstore进行项目,我有一个迁移文件: class CreateShippingCategories < ActiveRecord::Migration def change create_table :shipping_categories do |t| t.string :name t.hstore :size t.integer 我正在使用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  endend

默认情况下我有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_createend

但是当我尝试运行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的文档.

总结

以上是内存溢出为你收集整理的ruby-on-rails – rake db:seed不起作用全部内容,希望文章能够帮你解决ruby-on-rails – rake db:seed不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1276160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存