# TypeError on sef-referential association

**URL:** <https://www.ruby-forum.com/t/typeerror-on-sef-referential-association/109222>\
**Category:** Rails\
**Created:** [July 12, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/typeerror-on-sef-referential-association/109222 "2007-07-12T15:19:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitalex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7cd45c/32.png) [@digitalex](https://www.ruby-forum.com/u/digitalex)\
**Post date:** [July 12, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/typeerror-on-sef-referential-association/109222/1 "2007-07-12T15:19:14Z")

</div>

I have a self-referential association using the following two models:

class User \< ActiveRecord::Base  
has\_many :incoming\_friendships, :foreign\_key  
=\> :user1\_id, :class\_name =\> :friendship  
has\_many :outgoing\_friendships, :foreign\_key  
=\> :user2\_id, :class\_name =\> :friendship  
has\_many :incoming\_friends, :through =\> :outgoing\_friendships  
has\_many :outgoing\_friends, :through =\> :incoming\_friendships  
#…  
end

class Friendship \< ActiveRecord::Base  
belongs\_to :user1, :class\_name =\> :user, :foreign\_key =\> :user1\_id  
belongs\_to :user2, :class\_name =\> :user, :foreign\_key =\> :user2\_id  
end

In theory (according to josh;  
[has\_many :through - Self-referential has\_many :through associations](http://blog.hasmanythrough.com/2006/4/21/self-referential-through))  
this should work fine. However, I’m having some trouble…

When using the console, I try the command  
“User.find(1).incoming\_friends”. This results in a TypeError;

> > User.find(1).incoming\_friends  
> > TypeError: can’t convert Symbol into String  
> > from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/  
> > lib/active\_record/base.rb:1087:in `type_name_with_module' from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/ lib/active_record/base.rb:1356:in `compute\_type’  
> > from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/  
> > lib/active\_record/reflection.rb:125:in `send' from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/ lib/active_record/reflection.rb:125:in `klass’  
> > from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/  
> > lib/active\_record/reflection.rb:177:in `source_reflection' from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/ lib/active_record/reflection.rb:177:in `collect’  
> > from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/  
> > lib/active\_record/reflection.rb:177:in `source_reflection' from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/ lib/active_record/reflection.rb:186:in `check\_validity!’  
> > from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/  
> > lib/active\_record/associations/has\_many\_through\_association.rb:6:in  
> > `initialize' from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/ lib/active_record/associations.rb:934:in `new’  
> > from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/  
> > lib/active\_record/associations.rb:934:in `incoming\_friends’  
> > from (irb):1

If anyone has a clue what I might be doing wrong, I’d greatly  
appreciate some help!

---

<div class="post-metadata">

**Author:** ![digitalex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7cd45c/32.png) [@digitalex](https://www.ruby-forum.com/u/digitalex)\
**Post date:** [July 13, 2007, 10:58am UTC](https://www.ruby-forum.com/t/typeerror-on-sef-referential-association/109222/2 "2007-07-13T10:58:45Z")

</div>

I figured it out with help from somewhere else…for those who might  
be interested the solution was to name classnames and column names  
with strings, not symbols. It puzzles me why, but at least that solved  
my problem. So the two finished models looks like this:

class User \< ActiveRecord::Base  
#Friendship relations  
has\_many :incoming\_friendships, :class\_name =\>  
“Friendship”, :foreign\_key =\> “user1\_id”  
has\_many :outgoing\_friendships, :class\_name =\>  
“Friendship”, :foreign\_key =\> “user2\_id”  
has\_many :incoming\_friends, :through  
=\> :outgoing\_friendships, :source =\> :user1  
has\_many :outgoing\_friends, :through  
=\> :incoming\_friendships, :source =\> :user2

def friends  
incoming\_friends.find(:all, :conditions =\> :confirmed != nil) +  
outgoing\_friends.find(:all, :conditions =\> :confirmed != nil)  
end  
end

class Friendship \< ActiveRecord::Base  
belongs\_to :user1, :class\_name =\> “User”, :foreign\_key =\> “user1\_id”  
belongs\_to :user2, :class\_name =\> “User”, :foreign\_key =\> “user2\_id”  
end
