2012-07-09 49 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