Rails 请问 self-join 的问题。实现 twitter 的 follower 和关注

gazeldx · 2011年12月19日 · 最后由 suupic 回复于 2012年02月04日 · 3833 次阅读

用户表 users 我设计了一个关联表 follows

 create_table "follows", :id => false, :force => true do |t|
    t.integer  "me"
    t.integer  "you"
    t.datetime "created_at"
end

请问“这里”怎么写?

class User < ActiveRecord::Base
 #这里
end
class Follow < ActiveRecord::Base
 #这里 
end
belongs_to :xxx
has_many :xxx

我用 mongoid 的写法

has_and_belongs_to_many :following, 
                        :class_name => 'User', 
                        :inverse_of => :followers
has_and_belongs_to_many :followers, 
                        :class_name => 'User', 
                        :inverse_of => :following
需要 登录 后方可回复, 如果你还没有账号请 注册新账号