耙任务 - 未定义的方法

时间:2013-02-13 23:05:39

标签: ruby-on-rails ruby ruby-on-rails-3 rake koala

我正在修改我的方法来创建一个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

1 个答案:

答案 0 :(得分: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

然后将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.count_checkins(column.name)
    end
end

这样,count_checkins是在用户模型上定义的,而不是尝试修改Koala中的类 - 而且你不必通过传递更多用户和Facebook参数来复制工作。