2011-05-03 13 views
1

我有一個用戶表,並且每個用戶有另一個用戶,他們必須「殺死」關聯寫入如下。Rails-Belongs_to同一表內

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' 

在此先感謝您的幫助。

回答

6

將其更改爲:

belongs_to :user_to_kill, :class_name => "User"