查询自我轨道中的相关表

时间:2012-08-27 00:00:56

标签: ruby-on-rails

我有以下关系:

class User 
  has_many :followed_users, :through => :relationships, source: :followed

当我在控制台中说User.last.followed_users时,这是有效的。但是,当我在使用User的{​​{1}}类中创建方法时,这会失败。我希望能够在实现它的模型中调用self.followed_users。有什么帮助吗?

错误消息表示方法followed_users不存在。

1 个答案:

答案 0 :(得分:0)

如何定义关注者/以下关系集:

class User
  has_many :followings
  has_many :followers, :through => :followings
  has_many :inverse_followings, :class_name => "Following", :foreign_key => "follower_id"
  has_many :followed_users, :through => :inverse_followings, :source => :user
end

确保你有一个Follow class(或你的例子的Relationship类),它有一个user_id和一个follower_id来表达这种关系。

在User类方法中调用self.followed_usersself.followers应该可以正常工作。