0

我修改了创建一个 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

4

1 回答 1

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 中修改一个类——而且您不必通过传递不必要的更多 User 和 Facebook 参数来重复工作。

于 2013-02-13T23:42:19.127 回答