Rails:活动记录销毁时出现未初始化的常量错误

发布于 2024-07-16 08:25:52 字数 690 浏览 4 评论 0原文

我在尝试销毁活动记录实例时遇到问题。

它涉及以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城挽邺 2024-07-23 08:25:52

它没有找到您的指令模型。 确保它位于 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 the belongs_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.

娇女薄笑 2024-07-23 08:25:52

还要检查文件名是否与类名相对应。 就我而言

Class NameSpace::MyStats

namespace/old_stats.rb

Rails 一直抛出“未初始化的常量错误”,直到我将其更改为

namespace/my_stats.rb

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

namespace/my_stats.rb
星星的軌跡 2024-07-23 08:25:52

就我而言,由于复数,它没有找到正确的类名。 因此,我在关联中明确指定了类名。

对于你来说,它看起来像:

has_many :instructions, class_name: "Instruction", :dependent => :destroy

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:

has_many :instructions, class_name: "Instruction", :dependent => :destroy
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文