编辑: 解决了
has_many :imagens, :class_name => 'Imagem', :dependent => :delete_all
我面临的问题看起来很像这个问题:Rails : uninitialized constant error on Active Record destroy
我的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
答案 0 :(得分:1)
我刚才遇到了同样的问题并找到了答案:rails的惯例。在创建模型时以及在使用has_many:through / belongs_to时,您需要遵循它们。
class Imagem < ActiveRecord::Base
belongs_to :veiculo
# more irrelevant code, has attached file
end
应该是:
belongs_to :veiculos
因为“veiculo”.pluralize给了我们“veiculos”(你可以在你的控制台中测试它!)。这应该避免需要:class_name,我个人不喜欢。 :)
另外,如果你错过了它(我做了两次),你需要在你的关系表中使用单数名称,比如belongs_to:veiculo和belongs_to:imagem。