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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment