Search code examples
ruby-on-railsrubyruby-on-rails-3rakekoala

Rake task - undefined method


I tinkering my way into creating a rake task that grabs the amount of checkins for a given page throw facebook-graph. I usign the koala gem and rails.

I do this by creating a rake task:

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

But I get a error:

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

Any ideas or better way to code a rake task would be awesome!

Check the full error here: https://gist.github.com/shuma/4949213


Solution

  • Can't really format this properly in a comment, so I'll put it in an answer. I would put the following into the User model:

    # 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
    

    Then change the rake task to:

    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
    

    That way count_checkins is defined on the user model, rather than trying to modify a class within Koala -- and you aren't duplicating work by having to pass around more User and Facebook parameters than are necessary.