I'm running a Rails app on Postgres through Heroku.
I'd like to implement similar to Facebook "likes" on my site for various items, such as user comments. What's the smartest way to store these in my database that will be efficient and fast?
The obvious one is just to have a like join table between users and items, something like this:
user_id int
item_id int
item_type string
created_at datettime
However, when being displayed, this would mean every time I pull an item, I would have to pull a join across the entire like table, which could get very big.
The obvious response for this would be to store a counter in the items, for their ongoing like count. However, this won't work because who liked an item matters, both for display next to the item, and also to hide the like button for things a user has already liked.
My plan is to add to all likable items a text field in which I would store a serialized array. That way, every pull of an item would come with the complete list of who liked it. Is there a better way to do this, or is this the recommended approach?