Rails:活动记录销毁时出现未初始化的常量错误
我在尝试销毁活动记录实例时遇到问题。
它涉及以下 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
在客户端实例上执行销毁时,出现以下错误,
@dead_man = Client.find(params[:id])
@dead_man.destroy => uninitialized constant UserClient::Instruction
我真的不确定此错误来自何处。 任何帮助是极大的赞赏!
I am having an issue when trying to destroy an active record instance.
It involves the following 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它没有找到您的指令模型。 确保它位于 models 目录中,并适当命名、扩展
ActiveRecord::Base
等。此外,您应该删除
:dependent => :destroy
来自 UserClient 模型中的belongs_to :client
行,除非您确实希望删除 user_client 从而导致删除客户端。 听起来应该是相反的,并且这已经在客户端模型中设置了。It's not finding your Instruction model. Make sure it's in the models directory, appropriately named, extends
ActiveRecord::Base
, etc.Also, you should remove the
:dependent => :destroy
from thebelongs_to :client
line 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.还要检查文件名是否与类名相对应。 就我而言
,
Rails 一直抛出“未初始化的常量错误”,直到我将其更改为
Also check that the file name corresponds with the class name. In my case I had
in
and Rails kept on throwing the "uninitialized constant error" until I changed it to
就我而言,由于复数,它没有找到正确的类名。 因此,我在关联中明确指定了类名。
对于你来说,它看起来像:
In my case, it was not finding the correct class name because of pluralize. So, I specified the class name explicitly in my association.
For you, it would look like: