I have a model Follow that defines a user_id and a followed_user_id. If you've used Twitter, this should make sense.
I'm trying to make followed_user_id point to a User model, so I can access the user that is being followed through f.followed_user (in the same way that if I have an Entry with belongs_to :user and a user_id column I can use entry.user to get the user.)
How can I do this?
Check this screencast
It shows how a self referential association (what you are referring here) should be implemented in Rails.
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
@friendship = current_user.friendships.build(:friend_id => params[:friend_id])
flash[:notice] = "Added friend."
flash[:error] = "Unable to add friend."
@friendship = current_user.friendships.find(params[:id])
flash[:notice] = "Removed friendship."
You can do it like this:
belongs_to :followed_user, :class_name => "User"
This article walks you through exactly what you need to know on how to solve this issue:
Straight from the article:
class Sale < ActiveRecord::Base
belongs_to :buyer, :class_name => 'User', :foreign_key => 'buyer_id'
belongs_to :seller, :class_name => 'User', :foreign_key => 'seller_id'
class User < ActiveRecord::Base
has_many :purchases, :class_name => 'Sale', :foreign_key => 'buyer_id'
has_many :sales, :class_name => 'Sale', :foreign_key => 'seller_id'
The article explains in full what, why and how.