Rails-Belongs_to 在同一个表中

发布于 2024-11-04 21:16:46 字数 828 浏览 1 评论 0原文

我有一个用户表,每个用户都有另一个用户,他们必须“杀死”关联,如下所示。

class User < ActiveRecord::Base
  belongs_to(:user_to_kill, :class_name => :User)

但是,当我进行以下调用时:

@current_user.user_to_kill.screen_name

它返回以下错误和堆栈跟踪

NameError: uninitialized constant User::User
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/base.rb:1199:in `compute_type'
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/reflection.rb:162:in `klass'
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/associations/belongs_to_association.rb:59:in `find_target'
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/associations/association_proxy.rb:237:in `load_target'

提前感谢您的帮助。

I have a table of users and each user has another user that they have to "kill" the association is written as follows.

class User < ActiveRecord::Base
  belongs_to(:user_to_kill, :class_name => :User)

However when I make the following call:

@current_user.user_to_kill.screen_name

It returns the following error and stack trace

NameError: uninitialized constant User::User
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/base.rb:1199:in `compute_type'
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/reflection.rb:162:in `klass'
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/associations/belongs_to_association.rb:59:in `find_target'
    /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.0.3/lib/active_record/associations/association_proxy.rb:237:in `load_target'

Thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

掩于岁月 2024-11-11 21:16:46

将其更改为:

belongs_to :user_to_kill, :class_name => "User"

Change it to :

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