gpt4 book ai didi

ruby-on-rails - Rails 3 - has_many 关联未保存(TypeError)

转载 作者:行者123 更新时间:2023-12-04 00:03:38 25 4
gpt4 key购买 nike

Store has_many items, and an Item owns_to Store

我开了一家新店,保存起来,一切都很好。我检查我可以调用 .items 。

ruby-1.9.2-p290 :063 > s.items
Item Load (0.3ms) SELECT "items".* FROM "items" WHERE "items"."store_id" = 12
=> []

但是当我尝试调用 create 或 valid 时?它返回以下内容:
ruby-1.9.2-p290 :064 > s.items.create
Store Load (0.7ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = 12 LIMIT 1
TypeError: Cannot visit Store

我以前从未见过以下错误,所以我很头疼!

编辑:删除并重新迁移后,仍然得到以下内容:
    ruby-1.9.2-p290 :008 > Item.create(:store_id => 5)
Store Load (0.2ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = 5 LIMIT 1
TypeError: Cannot visit Store

最佳答案

万一其他人遇到这个问题,
结果证明是“项目”模型的验证问题。最初我有这个设置:

  validates :name, :presence => true, uniqueness => { :scope => :store }         

范围刚刚设置为:store。结果我需要明确说明:store_id:
  validates :name, :presence => true, uniqueness => { :scope => :store_id }         

关于ruby-on-rails - Rails 3 - has_many 关联未保存(TypeError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8463426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com