简体   繁体   English

Rails- Belongs_to 在同一个表中

[英]Rails- Belongs_to within same table

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.在此先感谢您的帮助。

Change it to:将其更改为:

belongs_to :user_to_kill, :class_name => "User"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM