Rails- Belongs_to在同一个表中

时间:2011-05-03 22:03:19

标签: ruby-on-rails activerecord associations

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

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'

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:6)

将其更改为:

belongs_to :user_to_kill, :class_name => "User"