New to RoR, trying to model this correctly (which should be super simple I''d think, but I''m stuck at the moment): Any given "User" of the app can have a group of "friends" (where friends are also users.) (think typical facebook friends concept.) I know I should have a ''user_friends'' table that has two user_ids as fks to the User table one for the user and the other to hold multiple ''friends'' My first shot is looking like this... class User < ActiveRecord::Base acts_as_authentic has_many :user_friends, :dependent => :destroy has_many :friends, :class_name => "User", :foreign_key => "friend_id", :through => :user_friends, :dependent => :destroy end class UserFriend < ActiveRecord::Base belongs_to :user belongs_to :user, :class_name => "User", :foreign_key => "friend_id" end #migration class CreateUserFriends < ActiveRecord::Migration def self.up create_table :user_friends, :id => false do |t| t.integer :user_id t.integer :friend_id t.timestamps end end When I run my test, it barfs saying Failures: 1) User should create a user and some friends Failure/Error: user.friends << f1 << f2 Could not find the source association(s) :user_friends in model UserFriend. Try ''has_many :friends, :through => :user_friends, :source => <name>''. Is it one of :user? Where my test looks like: it "should create a user and some friends" do user = Factory.create(:user_rick) f1 = Factory.create(:user_rachel) f2 = Factory.create(:user_fred) user.friends << f1 << f2 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Aug 30, 2010 at 7:06 PM, Rick R <rickcr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Any given "User" of the app can have a group of "friends" (where friends > are also users.) (think typical facebook friends concept.) > > I know I should have a ''user_friends'' table that has two user_ids as fks to > the User table one for the user and the other to hold multiple ''friends'' > > My first shot is looking like this... > > class User < ActiveRecord::Base > acts_as_authentic > has_many :user_friends, :dependent => :destroy > has_many :friends, :class_name => "User", :foreign_key => "friend_id", > :through => :user_friends, :dependent => :destroy > end > > class UserFriend < ActiveRecord::Base > belongs_to :user > belongs_to :user, :class_name => "User", :foreign_key => "friend_id" > end >Solved, I had to change UserFriend to: class UserFriend < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => "User", :foreign_key => "friend_id" end (Yes, I''ll remove the unnecessary foreign_key declarations as well.) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.