繁体   English   中英

耙任务-未定义的方法

[英]Rake task - undefined method

我修补方式创建了一个rake任务,该任务为给定的页面获取facebook-graph捕获了签入的数量。 我使用考拉宝石和护栏。

我通过创建一个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

不能真正在注释中正确设置其格式,因此我将其放在答案中。 我将以下内容放入用户模型:

# 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,而不是尝试在Koala中修改类-并且您不必通过传递多余的User和Facebook参数来复制工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM