ruby-on-railskoala

How to save Koala search results to Rails database


I have seen several short answers on how to initialize a connection to Facebook using koala and to then do a search using @graph.graph_call type method call, but I have not seen anyone explain how to then get the results from that search and save them into a Rails database. Do I have to create my own resource to match the json format that koala returns from Facebook and then save objects of those types into the rails database? Or, does koala have methods to handle that for me? Thanks.


Solution

  • You need to persist the fetched data on your own. You might need to create your own resource or add columns to existing tables. It depends on what you want to do.

    For example, Code for finding or creating a user by facebook user id will be look like this:

    graph = Koala::Facebook::API.new(user_fb_access_token)
    user_info = graph.get_object(:me, fields: %w(email))
    
    user = User.find_or_create_by(fb_user_id: fb_user_id) do |new_user|
      new_user.email = user_info['email']
      new_user.password = SecureRandom.hex
    end
    

    in this case, you need to add fb_user_id column to users table.