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)
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:intype_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:insend' 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:insource_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:insource_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!