I’m trying to implement a friends system on my site and just finished a railscast (#163) on self-referential associations (http://railscasts.com/episodes/163-self-referential-association). At one point in the video, he creates a new relationship with the inverse_friendships table, but I never see him generate a inverse_friendships model or migration. Does the inverse_ have to do with rails magic or is there something I missed?
models/user.rb
has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
Also, where does he get the friends column from? When he generated the scaffold for friendship, he created two columns user_id:integer friend_id:integer but didn’t have anything on an actual friends table.
generating scaffold
script/generate nifty_scaffold friendship user_id:integer friend_id:integer create destroy
rake db:migrate
There is no such table. The
:class_name => "Friendship"indicates that it is using theFriendshipmodel, which uses thefriendshipstable.