2013-02-13 38 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

相关问题