Search code examples
ruby-on-railsrubyfacebookkoalakoala-gem

How do i join the two records and return the table data?


I'm using the gem Koala to get facebook friends.

How to relate these results to the users of the database ?

code

facebook = Koala::Facebook::API.new(params[:facebook_access_token])
friends = facebook.get_connections("me", "friends")

return

[
  {
    "name": "Mick Fanning",
    "id": "9523891481361"
  },
  {
    "name": "Gabriel Medina",
    "id": "9523891483211"
  }
]

Idea:

join = friends.map {|u| {
  :user => user = User.where(provider_uid: u['id']).first,
  :provider_uid => u['id'],
  :name => u['name']
}}

This will generate lots of queries...

How do i join the two records and return the table data?


Solution

  • Anyway, it seems I got your need. Do the query first.

     users = User.where(provider_uid: friends.map { |h| h['id'] })
     users.map do |u|
       {
           :user => u,
           :provider_uid => u.id,
           :name => friends.find { |f| f['id'] == u.id }['name']
       }
     end