开源项目 ruby-china 的关注功能,数据库是怎样设计的?

stephen · 2012年07月14日 · 最后由 huacnlee 回复于 2012年10月19日 · 4493 次阅读

ruby-china 的关注功能,数据库是怎样设计的? 弱弱问问,ruby-china 所有的数据表结构放在哪里?

是用 MongoDB 的 user.rb field :favorite_topic_ids, :type => Array, :default => []

匿名 #2 2012年07月15日

分享下我最近的一个解决方案:

create_table "followships", :force => true do |t|
    t.integer  "member_id",       :null => false
    t.integer  "followable_id",   :null => false
    t.string   "followable_type", :null => false
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
end
current_member.followships.create(followable_type: "Member", followable_id: 1)

表示当前用户关注了 ID 为 1 的用户

current_member.followships.create(followable_type: "Article", followable_id: 2)

表示当前用户关注了 ID 为 2 的帖子

用到了 Polymorphic Associations 的知识: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

这里是一个运行中的网站,可以关注人,关注帖子,关注分类等等:http://www.petegy.com/members/tylerlong

我打算把这部分功能做一个 gem 开源出去,不过暂时没有时间表,可能要等很久。

@tylerlong 用户关注文章之类,文章不会反过来关注用户。但是用户和用户之间可以相互关注,是否合在一起不合适?

@tylerlong 用户关注帖子,是一个多对多的关联,按照你的方法,在 model 里面,如何描述他们的关系?has_and_belongs_to_many???

三位前部推荐的原理应该是一样!

@tylerlong @yedingding @fredwu 三位前部推荐的原理应该是一样!

匿名 #9 2012年07月15日

#6 楼 @stephen 比如 A follow B, followships 是中间表。A has_many followships, B has_many followships. 我当时的设计 A 只能是人,B 可以是任意 model. 具体有一点点复杂,建议你研究下http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

你的问题解决了吗?可否分享一下经验。

我是用 Redis 的 Sorted Set 来实现的,感觉比用关系数据库来实现要简单

#11 楼 @HungYuHei 哥们能帮忙找个教程 或者实例看看吗?thx

acts_as_follower 不错的!现在有两个项目就是用它来实现的

需要 登录 后方可回复, 如果你还没有账号请 注册新账号