Ruby on rails ActiveModel::命名空间模型的UnknownAttributeError
我有两个型号,它们的名称都在“Loot”下 战利品::屏幕快照收集 战利品:截图 当我试图用子屏幕快照创建一个新的屏幕快照集合时,我遇到了一个错误Ruby on rails ActiveModel::命名空间模型的UnknownAttributeError,ruby-on-rails,activerecord,namespaces,activemodel,Ruby On Rails,Activerecord,Namespaces,Activemodel,我有两个型号,它们的名称都在“Loot”下 战利品::屏幕快照收集 战利品:截图 当我试图用子屏幕快照创建一个新的屏幕快照集合时,我遇到了一个错误 ActiveModel::UnknownAttributeError (unknown attribute 'screenshot_collection_id' for Loot::Screenshot.) loot\u screenshots表上数据库中的外键是“loot\u screenshot\u collection\u id”,但Rail
ActiveModel::UnknownAttributeError (unknown attribute 'screenshot_collection_id' for Loot::Screenshot.)
loot\u screenshots表上数据库中的外键是“loot\u screenshot\u collection\u 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.)
2.5.1:016>collection=Loot::ScreenshotCollection.new
=> #
2.5.1:017>收集.截图.新建
回溯(最近一次呼叫最后一次):
1:from(irb):17
ActiveModel::UnknownAttributeError(ROOT::screenshot的未知属性'screenshot\u collection\u id'
您需要在两个文件中提及外键
:
在屏幕截图_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"
更改为
有很多:屏幕截图,类名:'Loot::Screenshot',foregin键:'Loot\u Screenshot\u collection\u id',dependent::destroy
has_many :screenshots, class_name: 'Loot::Screenshot', foreign_key: 'loot_screenshot_collection_id', dependent: :destroy
belongs_to :screenshot_collection, class_name:'Loot::ScreenshotCollection', foreign_key: "loot_screenshot_collection_id"