2012-07-09 100 views
0

我抓住值數據:名稱,UID,highschool_name,graduateschool_name這樣的:導軌 - 較好的流量控制 - 環

def add_friends 
    facebook.get_connections("me", "friends", :fields => "name, id, education").each do |hash| 
     self.friends.where(:name => hash['name'], 
         :uid => hash['id'], 
         :highschool_name => hash['education']['school']['name'] unless hash["education"].blank?, 
         :graduateschool_name => hash['education']['school']['name'] unless hash["education"].blank?). 
        first_or_create 
    end 
end 

從散列的數組:

"education": [ 
    { 
     "school": { 
     "id": "110703012290674", 
     "name": "Kunskapsgymnasiet Malmö" 
     }, 
     "year": { 
     "id": "136328419721520", 
     "name": "2009" 
     }, 
     "type": "High School" 
    }, 
    { 
     "school": { 
     "id": "112812485399398", 
     "name": "Malmö University" 
     }, 
     "year": { 
     "id": "118118634930920", 
     "name": "2012" 
     }, 
     "concentration": [ 
     { 
      "id": "104076956295773", 
      "name": "Computer Science" 
     } 
     ], 
     "type": "Graduate School", 
     "classes": [ 
     { 
      "id": "165093923542525", 
      "name": "Programmering", 
      "description": "Kursen fokuserar på metoder och tekniker vid utveckling av webbapplikationer med hjälp av HTML5." 
     } 
     ] 
    } 
    ], 

編輯: 這代碼劑量工作。我想從這個哈希數組中選擇所有的學校和研究生院並保存它。

+0

我以前見過這個散列。 :)是不是我以前的答案對你有用?你在問別的嗎? – 2012-07-09 22:53:27

+0

@Sergio Tulentsev,我有但並非一樣。我試圖在這個方法上實現同樣的tatice,但沒有任何結果。 – SHUMAcupcake 2012-07-09 22:56:15

+0

此代碼不起作用,對吧?描述它究竟如何不起作用。你希望它做什麼。 – 2012-07-09 22:59:31

回答

0
high_schools = response['education'].collect{|ed| ed['school']['name'] if ed['type'] == "High School" } 
grad_schools = response['education'].collect{|ed| ed['school']['name'] if ed['type'] == "Graduate School" } 
+0

你可以給更多indeep解釋? – SHUMAcupcake 2012-07-09 22:58:53

+0

此方法使用內置的Array.collect方法將所有高中名稱收集到一個數組中;然後爲研究生院的名字也做同樣的事情。 – steakchaser 2012-07-09 23:02:58

+0

我應該在哪個方法中放置它? – SHUMAcupcake 2012-07-09 23:04:00