gpt4 book ai didi

ruby-on-rails - 命名空间模型的 ActiveModel::UnknownAttributeError

转载 作者:行者123 更新时间:2023-12-04 17:01:51 30 4
gpt4 key购买 nike

我有 2 个模型,它们都在“Loot”下命名
战利品::截图收藏

belongs_to :agent
has_many :screenshots, class_name: 'Loot::Screenshot', dependent: :destroy
accepts_nested_attributes_for :screenshots, allow_destroy: :true
战利品::截图
belongs_to :screenshot_collection, class_name:'Loot::ScreenshotCollection', foreign_key: "loot_screenshot_collection_id"
当我尝试使用子屏幕截图创建新的 ScreenshotCollection 时出现错误
ActiveModel::UnknownAttributeError (unknown attribute 'screenshot_collection_id' for Loot::Screenshot.)
loot_screenshots 表中数据库中的外键是“loot_screenshot_collection_id”,但由于某种原因,Rails 不理解这一点并查找没有前缀的表名。
Rails 控制台示例:
2.5.1 :016 > collection = Loot::ScreenshotCollection.new
=> #<Loot::ScreenshotCollection id: nil, created_at: nil, updated_at: nil>
2.5.1 :017 > collection.screenshots.new
Traceback (most recent call last):
1: from (irb):17
ActiveModel::UnknownAttributeError (unknown attribute 'screenshot_collection_id' for Loot::Screenshot.)

最佳答案

您需要提及 foreign_key在两个文件中:

在screenshot_collection.rb

has_many :screenshots, class_name: 'Loot::Screenshot', foreign_key: 'loot_screenshot_collection_id', dependent: :destroy

screenshot.rb :
belongs_to :screenshot_collection, class_name:'Loot::ScreenshotCollection', foreign_key: "loot_screenshot_collection_id"

关于ruby-on-rails - 命名空间模型的 ActiveModel::UnknownAttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50889814/

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