I have more or less the same problem. I’m trying to build a system where
users can enter their friends. But the solutions seems not to work for
me.
I used same sql:
CREATE TABLE friends
(
user_id
int(11) NOT NULL default ‘0’,
friend_id
int(11) NOT NULL default ‘0’,
KEY user_id
(user_id
),
KEY friend_id
(friend_id
),
CONSTRAINT friends_ibfk_1
FOREIGN KEY (user_id
) REFERENCES
users
(id
),
CONSTRAINT friends_ibfk_2
FOREIGN KEY (friend_id
) REFERENCES
users
(id
)
)
My user.rb looks like this:
class User < ActiveRecord::Base
has_and_belongs_to_many :friends, :join_table => ‘friends’,
:class_name => ‘User’, :foreign_key => ‘friend_id’
has_and_belongs_to_many :friends_of, :join_table => ‘friends’,
:class_name => ‘User’, :foreign_key => ‘user_id’,
:association_foreign_key => ‘friend_id’
attr_accessor :new_password
attr_accessor :friends
attr_accessor :friends_of
…
end
But when I use user.friends or user.friends_of they always return nil
even after I entered some data in the db manually. There are no errors
in the logfile…
Any ideas?
thanks
Frank