我有以下关系:
class User
has_many :followed_users, :through => :relationships, source: :followed
当我在控制台中说User.last.followed_users
时,这是有效的。但是,当我在使用User
的{{1}}类中创建方法时,这会失败。我希望能够在实现它的模型中调用self.followed_users
。有什么帮助吗?
错误消息表示方法followed_users
不存在。
答案 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_users
或self.followers
应该可以正常工作。