Ruby-on-rails Rails:活动记录销毁时未初始化的常量错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/727801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails : uninitialized constant error on Active Record destroy
提问by stellard
I am having an issue when trying to destroy an active record instance.
我在尝试销毁活动记录实例时遇到问题。
It involves the following AR
它涉及以下AR
class Client < ActiveRecord::Base
has_many :phone_numbers, :dependent => :destroy
has_many :email_addresses, :dependent => :destroy
has_many :user_clients , :dependent => :destroy
has_many :users, :through => :user_clients
end
class UserClient < ActiveRecord::Base
belongs_to :user
belongs_to :client , :dependent => :destroy
has_many :instructions, :dependent => :destroy
end
When performing a destroy on a Client instance I am given the following error
在客户端实例上执行销毁时,出现以下错误
@dead_man = Client.find(params[:id])
@dead_man.destroy => uninitialized constant UserClient::Instruction
I am really not sure where this error is coming from. Any help is greatly appreciated!
我真的不确定这个错误是从哪里来的。任何帮助是极大的赞赏!
回答by Sarah Mei
It's not finding your Instruction model. Make sure it's in the models directory, appropriately named, extends ActiveRecord::Base, etc.
它没有找到您的指令模型。确保它在模型目录中,适当命名,扩展ActiveRecord::Base等。
Also, you should remove the :dependent => :destroyfrom the belongs_to :clientline in the UserClient model, unless you really want deletion of a user_client to result in deletion of the client. It sounds like it should be the other way around, and that's already set up in the Client model.
此外,您应该:dependent => :destroy从belongs_to :clientUserClient 模型中的行中删除 ,除非您确实希望删除 user_client 以导致删除客户端。听起来应该是相反的,这已经在客户端模型中设置了。
回答by Maragues
Also check that the file name corresponds with the class name. In my case I had
还要检查文件名是否与类名相对应。就我而言,我有
Class NameSpace::MyStats
in
在
namespace/old_stats.rb
and Rails kept on throwing the "uninitialized constant error" until I changed it to
并且 Rails 一直抛出“未初始化的常量错误”,直到我将其更改为
namespace/my_stats.rb

