There are a number of posts and threads on has_many :through, but I haven\'t found any that cover specifically what I\'m trying to do.
I have a User model and a Fri
I'm kind of a noob in a learning process, but this models seem cleaner to me:
class User < ActiveRecord::Base
has_many :followings
has_many :followers, through: :followings
end
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :follower, class_name: 'User'
end
You need to make sure ActiveRecord knows what the source association for the User#friends and likewise the followers and specify the class and foreign_key for the relationships that ActiveRecord can't extrapolate from the association names.
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :followed, :class_name => 'User'
end
class User < ActiveRecord::Base
has_many :followings
has_many :friends, :through => :followings, :source => 'followed'
has_many :followeds, :class_name => 'Following', :foreign_key => 'followed_id'
has_many :followers, :through => :followeds, :source => :user
end