新手问题 收藏功能的设计查询方案

luffycn · October 06, 2014 · 1079 hits

有两个表 Article: id, title, content UserCollection: id, collectable_id, collectable_type (这里用了多态)

想要判断当前文章 是否有没有被用户收藏过,可以在 user model 里写一个 def has_collected?(article) end 里面用 where 查询来解决

有没有更简洁的办法,比如 scope 或者 其它更少代码的方案,不用再多写一个方法的方式来解决这个问题了

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.