I have a Ruby on Rails app that allows the user to login with their Facebook account. I want to be able to a list of users and their Facebook data. I have been able to accomplish this however I have to send a request for each user’s name and I want to do this as a batch operation. How would I be able to put this in my user model?
User Model
class User < ActiveRecord::Base
attr_accessible :uid
def facebook_name
Koala::Facebook::API.new.get_object(uid)["name"].to_s
end
end
Example of Inefficient code
users = Users.all
users.each do |user|
puts user.facebook_name
end
Use a background task (such as https://github.com/collectiveidea/delayed_job) to populate the data in the database.
For example
Then, once you’ve done that, it’s just a regular model database to iterate through them, as the fields will be cached in your DB. You really don’t want to be making HTTP api requests during responding to another HTTP request.