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
On 1/6/06, Frank Spychalski <rails-ml@spychalski.de> wrote:> 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? >You don''t need attr_accessors for associations. has_many :whatevers creates the ''whatevers'' method for you. P.S. You can do multiple attr_accessors on one line, separated with commas, if you want. e.g. attr_accessor :thing1, :thing2, :thing3
Wilson Bilkovich wrote:> > You don''t need attr_accessors for associations. has_many :whatevers > creates the ''whatevers'' method for you. > > P.S. You can do multiple attr_accessors on one line, separated with > commas, if you want. > e.g. attr_accessor :thing1, :thing2, :thing3thanks - the accessors where leftovers from a previous try and I forgot to remove them... Frank