Ruby on rails Rails活动记录:有许多错误(NameError:未初始化常量)

Ruby on rails Rails活动记录:有许多错误(NameError:未初始化常量),ruby-on-rails,testing,activerecord,Ruby On Rails,Testing,Activerecord,编辑: 解决 has_many :imagens, :class_name => 'Imagem', :dependent => :delete_all 我面临的问题很像这样: 我的iflections文件包含以下内容: inflect.plural 'imagem', 'imagens' inflect.singular 'imagens', 'imagem' 当我尝试获取图像时,会出现以下错误: veiculo = Veiculo.first veiculo.imagens

编辑: 解决

has_many :imagens, :class_name => 'Imagem', :dependent => :delete_all

我面临的问题很像这样:

我的iflections文件包含以下内容:

inflect.plural 'imagem', 'imagens'
inflect.singular 'imagens', 'imagem'
当我尝试获取图像时,会出现以下错误:

veiculo = Veiculo.first
veiculo.imagens #uninitialized constant Veiculo::Imagen
我不知道为什么会这样

class Veiculo < ActiveRecord::Base
  has_many :caracteristicas, :dependent => :delete_all
  has_many :imagens, :dependent => :delete_all
  # more irrelevant code
end

class Imagem < ActiveRecord::Base
  belongs_to :veiculo
  # more irrelevant code, has attached file
end
class-Veiculo:全部删除
有多个:映像,:依赖=>:全部删除
#更无关的代码
结束
类Imagem
我刚才遇到了同样的问题,并找到了答案:rails的约定。在创建模型和使用模型中的has\u many:through/belling\u to时,您需要遵循它们

class Imagem < ActiveRecord::Base
  belongs_to :veiculo
  # more irrelevant code, has attached file
end
因为“veiculo”。pluralize给了我们“veiculo”(你可以在你的控制台中测试!)。这应该避免需要:class_name,我个人不喜欢它。:)

此外,如果您错过了它(我错过了两次),您需要在关系表中使用单数名称,如belishto to:veiculo和belishto to:imagem

belongs_to :veiculos