1

基本上,我在我的 Rails 应用程序中有一个用户模型和一个粉丝模型,以促进用户成为彼此的“粉丝”。

在我的用户模型中,我有:

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

在我的粉丝模型中,我有:

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

我的粉丝表基本上由:id、:user_id 和:fanof_id 组成。这一切都很好,我可以看到特定用户喜欢哪些用户:

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

我的问题是,我怎样才能得到这个特定用户的粉丝的用户列表?

如果我可以拥有像@user.fans 这样的东西,我会很高兴,但如果这不可能,那么最有效的方法是什么?

谢谢!

4

1 回答 1

2

添加用户模型:

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

(未测试)

于 2010-05-04T16:40:37.553 回答