2013-02-13 69 views
0

我修補我的方法來創建一個耙子任務,抓取給定頁面的簽入量扔臉譜。我使用考拉寶石和鐵軌。Rake任務 - 未定義的方法

我這樣做是通過創建一個rake任務:

task :get_likes => :environment do 
    require 'koala' 
    # Grab the first user in the database 
    user = User.first 

    # Loop throw every school & and call count_checkins 
    School.columns.each do |column| 
     user.facebook.count_checkins(column.name, user) 
    end 
end 
# Count like for every school else return 0 
def count_checkins(name, u) 
    a = u.facebook.fql_query('SELECT checkins FROM page WHERE name = "' + name + '"') 
    if a[0].nil? 
     return 0 
    else 
     return b = a[0]["checkins"] 
    end 
end 
# Initialize an connection to the facebook graph 
def facebook 
    @facebook ||= Koala::Facebook::API.new(oauth_token) 
end 

但我得到一個錯誤:

private method `count_checkins' called for #<Koala::Facebook::API:0x007fae5bd348f0> 

任何意見或更好的方式來編寫一個rake任務將是真棒!

檢查完整的錯誤在這裏:https://gist.github.com/shuma/4949213

+0

最後2個方法定義在哪裏? – 2013-02-13 23:16:38

+0

@ mind.blank在rake文件中。 – SHUMAcupcake 2013-02-13 23:19:53

+0

您需要在相關的類定義中定義它們。例如'user.facebook'將嘗試調用User類的'facebook'方法,但是您定義的方法不附加到該類。 – 2013-02-13 23:22:11

回答

0

真的不能格式化這個正確的評論,所以我把它放在一個答案。我把下面進入用戶模式:

# Count like for every school else return 0 
def count_checkins(name) 
    a = self.facebook.fql_query('SELECT checkins FROM page WHERE name = "' + name + '"') 
    if a[0].nil? 
     return 0 
    else 
     return b = a[0]["checkins"] 
    end 
end 

# Initialize an connection to the facebook graph 
def facebook 
    @facebook ||= Koala::Facebook::API.new(oauth_token) 
end 

然後改變耙任務:

task :get_likes => :environment do 
    require 'koala' 
    # Grab the first user in the database 
    user = User.first 

    # Loop throw every school & and call count_checkins 
    School.columns.each do |column| 
     user.count_checkins(column.name) 
    end 
end 

這樣count_checkins定義的用戶模型,而不是試圖修改內考拉類 - 而且您不必複製工作,因爲必須傳遞更多的用戶和Facebook參數。

+0

對於'School.columns'的內容我是不知道的,不管它是否有意義,但它至少應該解決未定義的方法問題。 – 2013-02-13 23:44:07

+0

不錯的想法,但它仍然劑量找到方法。未定義的方法'count_checkins' – SHUMAcupcake 2013-02-13 23:51:10

+0

用戶類定義('class User'和'end'之間)是否有新的'def count_checkins'塊?另外,你應該檢查一下,確保你實際上在你的rake任務中找到了用戶記錄;你有可能得到零嗎? – 2013-02-14 00:26:38

相關問題