开发者

Rails query to find other likes based on category

I need to do something like the following:

Find other categories people like based on the categories you like. I have a likes table, which is joined to users and categories.

I need to do an efficient query to find out what other categories people who liked a given category also like.

开发者_运维技巧

Any thoughts on this would be appreciated. Thanks in advance.


You want to find all of the users who have all of the categories that current user has then collect all of the categories that the others have with the exclusion of the categories that current user has.

How about something like this:

@user_group =  User.find(:select => "count(likes) as count, users.*", 
                          :joins => :likes,
                          :conditions => {:likes => {:category_id => current_user.categories.map(&:category_id).flatten}}
                          :group => "#{('users.id having count = ?', current_user.categories.count)}")
@categories = Categories.find(:joins => :likes, :conditions => ['likes.user_id in ? and id not in ?', @user_group.map(&:id), current_user.categories.map(&:id)]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜