I need to sort activerecord data by facebook likes. I assume this means that I will need to add a likes field to my model and then populate this with the number of likes that objects related page has had.
Just wondering if there is a better way to do this and how I would go about populating the likes column in the db with the number of likes. Just looking for the best method to do this really.
My current idea is to create the likes field and try and populate it from json object returned from facebookgraph so I can use the activerecord order method to sort. Just not sure if this is the best way and the best way to get the data into the db.
Any help would be much appreciated.
There doesn’t seem to be anything wrong with what you are suggesting, but you should bear in mind that storing the number of likes in a model is going to require you to update that frequently with the number of likes from the Facebook Graph API (which is what I assume that you are using).
It might be better to not store the number of likes directly in your application, but simply pull them when your app needs them – this way you will know they are up-to-date, and a post hasn’t been liked/unliked since you last polled.