I want to build a query in Rails 3.2 (MYSQL), where I sort a group of posts by the number of your friends that are interacting with it. I can limit this to last 7 days.. My current thinking is:
-
Maintain the following tables / relationships:
Users has many Friends
Post has many Interactors -
__ 🙂 I’m not quite sure how to most efficiently build the query. Perhaps, naively Id’ think I’d first query to get the users friends.. put that into an array.. then query the interactors (last 7 days), where the user_id is ‘in’ my friends list.. in this query, lazy load the posts as well.. then on the front end count the number of interactors per post.. attach that to the posts.. and then sort them? Must be a more efficient way..
Thanks!
Assuming there is a
Friendtable containingUserIDandFriendID, and aPostInteractiontable that containsPostIDandUserID, you would:I have assumed the same friend could have multiple interactions with a post and that you only want to count that once, thus the use of
DISTINCTin the count.