Ruby on rails Rake任务-未定义的方法
我修补了我的方法,创建了一个rake任务,它可以获取给定页面的签入量。我用考拉宝石和栏杆 我通过创建一个rake任务来实现这一点:Ruby on rails Rake任务-未定义的方法,ruby-on-rails,ruby,ruby-on-rails-3,rake,koala,Ruby On Rails,Ruby,Ruby On Rails 3,Rake,Koala,我修补了我的方法,创建了一个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
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>
调用了私有方法“count\u checkins”#
任何编写rake任务的想法或更好的方法都将非常棒
检查此处的完整错误:无法在注释中正确格式化此内容,因此我会将其放入答案中。我将在用户模型中添加以下内容:
# 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_签入,而不是试图修改考拉中的一个类——你也不需要通过传递更多的用户和Facebook参数来重复工作。最后两个方法在rake文件中定义在哪里?@mind.blank。你需要在相关的类定义中定义它们。e、 g.
user.facebook
将尝试调用user类上的facebook
方法,但您定义的方法未附加到该类。@SeanD。当我这样做时,我得到了这个错误:未定义的方法count\u checkins。尝试执行def self.count\u checkins(name)
并将其放入用户模型中。考虑到School.columns
的内容,这是否真的有意义我不知道,但至少应该解决未定义的方法问题。好主意,但它仍然没有找到方法。未定义的方法“count\u checkins”是用户类定义中的新def count\u checkins
块(介于class User
和end
之间)?此外,您可能应该检查以确保您在rake任务中实际找到了用户记录;你有可能得到零分吗?我发现了几个错误,我修复了它。我喜欢你的代码,干得好伙计!