Alex
Alex

Reputation: 730

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.

Upvotes: 1

Views: 755

Answers (1)

Spyros
Spyros

Reputation: 48706

Change it to :

belongs_to :user_to_kill, :class_name => "User"

Upvotes: 6

Related Questions