Accessing two sides of a user-user relationship in rails
Posted
by Lowgain
on Stack Overflow
See other posts from Stack Overflow
or by Lowgain
Published on 2010-05-04T16:22:27Z
Indexed on
2010/05/04
16:28 UTC
Read the original article
Hit count: 164
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!
© Stack Overflow or respective owner