Hi,
I have a self-referential has_many :through relationship setup to
track relationships between users. Basically relationships are
modeled as a join table with an extra column ‘relation’.
create table relationships (
user_id integer unsigned not null,
friend_id integer unsigned not null,
relation char(1) not null,
)
— relations —
f = friend
r = request to be a friend
b = blocked
My models look like this:
class Relationship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => ‘User’, :foreign_key => ‘friend_id’
end
class User < ActiveRecord::Base
has_many :relationships
has_many :friendships, :class_name => ‘Relationship’, :foreign_key
=> ‘user_id’, :conditions => “relation = ‘f’”
has_many :potential_friendships, :class_name => ‘Relationship’,
:foreign_key => ‘user_id’, :conditions => “relation = ‘r’”
has_many :friends, :through => :friendships
has_many :potential_friends, :through => :potential_friendships
end
Everything works great except the last potential_friends line. It
seems unable to figure out to use the friend_id foreign key. I have
tried to specified the :source param according to the docs. Am I
missing something here? The exact error is:
User.find(1).potential_friends
ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
ActiveRecord::HasManyThroughSourceAssociationNotFoundError
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/reflection.rb:173:in
check_validity!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_through_association.rb:6:in
initialize’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:876:in
`potential_friends’
from (irb):4
By the way I have read the excellent article over at
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through.
Any ideas?
Thanks,
Zack
u.potential_friendships
=> [#<Relationship:0x247d44c @attributes={“relation”=>“r”,
“user_id”=>“1”, “friend_id”=>“22”}>, #<Relationship:0x247d410
@attributes={“relation”=>“r”, “user_id”=>“1”, “friend_id”=>“23”}>]
u.potential_friends
ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
ActiveRecord::HasManyThroughSourceAssociationNotFoundError
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/reflection.rb:173:in
check_validity!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_through_association.rb:6:in
initialize’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:876:in
`potential_friends’
from (irb):4
--------------- [ schema, classes] ---------------
create table relationships (
user_id integer unsigned not null,
friend_id integer unsigned not null,
relation char(1) not null,
foreign key (user_id) references users(id) on update cascade on
delete cascade,
foreign key (friend_id) references users(id) on update cascade on
delete cascade,
primary key(user_id, friend_id)
) engine=innodb character set utf8;
class Relationship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => ‘User’, :foreign_key => ‘friend_id’
end
class User < ActiveRecord::Base
has_many :relationships
has_many :friendships, :class_name => ‘Relationship’, :foreign_key
=> ‘user_id’, :conditions => “relation = ‘f’”
has_many :potential_friendships, :class_name => ‘Relationship’,
:foreign_key => ‘user_id’, :conditions => “relation = ‘r’”
has_many :friends, :through => :friendships
has_many :potential_friends, :through => :potential_friendships
end