2012-07-10 27 views
0

我建立了一個應用程序,用戶在那裏登錄扔有Facebook帳戶,我抓住他們的朋友和他們的教育歷史。它是這樣的:初始化朋友模型在軌道上

在用戶登錄並轉到SessionsController#創建:

class SessionsController < ApplicationController 
    def create 
    user = User.from_omniauth(env['omniauth.auth']) 
    end 
end 

的SessionsController方法調用創建在用戶模型的方法.from_omniauth:

def self.from_omniauth(auth) 
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user| 
     user.provider = auth["provider"] 
     user.uid = auth["uid"] 
     ...more code... 
     user.save! 
     user.add_friends 
    end 
end 

.from_omniauth方法調用User模型中的方法add_friends:

def add_friends 
    friends_data = facebook.get_connections("me", "friends", :fields => "name, id, education") 
    friends_data.each do |hash| 
     friend.name = hash["name"] 
     friend.uid = hash["id"] 

     if hash["education"] 
     hash["education"].each do |e| 
      if e["type"] == "High School" 
      friend.highschool_name = e["school"]["name"] if (!hash["education"].blank? && !e["school"].blank?) 

      elsif e["type"] == "Graduate School" 
      friend.graduateschool_name = e["school"]["name"] if (!hash["education"].blank? && !e["school"].blank?) 
      end 
     end 
     end 
     friend.save! 
     friend 
    end 
    end 

我得到這個錯誤:

NameError in SessionsController#create 
undefined local variable or method `friend' for #<User:0x007fad11d50eb0> 

我知道這意味着我必須初始化的變量的朋友,但我不知道如何做到這一點的想法。任何想法,這將是非常有益的! =)

回答

2

使用friend = Friend.new在循環:

friends_data.each do |hash| 
    friend = Friend.new # <----------- 
    friend.name = hash["name"] 
    friend.uid = hash["id"] 
+0

是啊,這工作,謝謝! – SHUMAcupcake 2012-07-10 14:50:29

+0

一個問題,我的朋友模型看起來像這樣:[uid,name,highschool_name,graduateschool_name,user_id,created_at,updated_at],但user_id是空的。所以我必須以其他方式保存任何想法?我的模式如下所示:https://gist.github.com/3083962 – SHUMAcupcake 2012-07-10 15:18:43

+0

取決於'user_id'的含義。如果這是當前用戶的ID,那麼使用'friend.user_id = self.id'。如果不是,請說明該字段的含義。 – Matzi 2012-07-10 15:25:22

0

首先,你需要朋友型號:

rails g model Friend user_id:integer uid:string name:string highschool_name:string graduateschool_name:string 
rake db:migrate 

在友元類把belongs_to的:

class Friend < ActiveRecord::Base 
    belongs_to :user 
end 

在用戶級放的has_many :

class User < ActiveRecord::Base 
    has_many :friends 
end 

現在你add_friend方法應該是這樣的:

def add_friends 
    friends_data = facebook.get_connections("me", "friends", :fields => "name, id, education") 
    friends_data.map do |hash| 
     friend=Friend.new 
     friend.name = hash["name"] 
     friend.uid = hash["id"] 
     friend.user_id = self.id 

     if hash["education"] 
     hash["education"].each do |e| 
      next if e["school"].blank? 
      if e["type"] == "High School" 
      friend.highschool_name = e["school"]["name"] 
      elsif e["type"] == "Graduate School" 
      friend.graduateschool_name = e["school"]["name"] 
      end 
     end 
     end 
     friend.save! 
     friend 
    end 
    end 
+0

我有一個這樣的朋友模型,但用out_key =>:uid屬性,爲什麼你使用那個doesent活動記錄自動處理? – SHUMAcupcake 2012-07-10 14:55:16

+0

Rails默認猜測'foreign_key'是一個「_id」關聯的名字,所以在你的模型中它將是'user_id'而不是'uid'。看看:[ActiveRecord :: Associations :: ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html) – rogal111 2012-07-10 14:58:09

+1

你是對的,但我的模式如下所示:https:/ /gist.github.com/3083962我注意到索引中沒有任何內容。 – SHUMAcupcake 2012-07-10 15:16:15