views:

34

answers:

1

Basically, I have a users model in my rails app, and a fanship model, to facilitate the ability for users to become 'fans' of each other.

In my user model, I have:

has_many :fanships
has_many :fanofs, :through => :fanships

In my fanship model, I have:

belongs_to :user
belongs_to :fanof, :class_name => "User", :foreign_key => "fanof_id"

My fanship table basically consists of :id, :user_id and :fanof_id. This all works fine, and I can see what users a specific user is a fan of like:

<% @user.fanofs.each do |fan| %>
    #things
<% end %>

My question is, how can I get a list of the users that are a fan of this specific user?

I'd like it if I could just have something like @user.fans, but if that isn't possible what is the most efficient way of going about this?

Thanks!

+2  A: 

Add in User model:

has_many :my_fanclubs, :class_name => 'Fanship', :foreign_key => 'fanof_id'
has_many :fans, :through => :my_fanclubs, :source => :user, :class_name => 'User'

(not tested)

klew
In the second line, I changed :source => 'user_id' to :source => :user, and it worked! thanks
Lowgain
Thanks, I corrected it.
klew