即使我包含环境,object.find 在 rakefile 中也会失败
我有一个包含 cron 作业的 rakefile,它访问我的模型之一,然后对我的数据库进行搜索。我包括我的环境并希望能够创建我的对象,但是当我尝试运行“查找”方法时,我收到此错误:
rake aborted!
undefined method `find' for #<Feeder:0x103611df0>
/Library/Ruby/Gems/1.8/gems/activemodel-3.0.4/lib/active_model/attribute_methods.rb:364:in `method_missing'
/Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/attribute_methods.rb:46:in `method_missing'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:28
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:20:in `each'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:20
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:18:in `each'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:18
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
以下是代码:
require 'rails/all'
require 'rake/clean'
require File.expand_path('../config/application', __FILE__)
require 'rubygems'
require 'sqlite3'
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
puts "Running cron at #{Time.now.strftime('%Y/%m/%d %H:%M:%S')}..."
require 'app/models/feeder'
num_feeds = 10
feedurls = ["http://twitter.com/statuses/user_timeline/2205491.rss","http://github.com/joshkerr.atom","http://gdata.youtube.com/feeds/base/users/joshkerr/uploads","http://api.flickr.com/services/feeds/photos_public.gne?id=95219360@N00&lang=en-us&format=rss_200","http://www.google.com/reader/public/atom/user%2F00755727578217480286%2Fstate%2Fcom.google%2Fbroadcast"]
feedurls.each do |feed|
rawfeed = Feedzirra::Feed.fetch_and_parse(feed)
for i in 0..num_feeds do
# Database call
fed = Feeder.new
fed.title = rawfeed.entries[i].title
fed.published = rawfeed.entries[i].published
fed.url = rawfeed.entries[i].url
fed.feed_id = rawfeed.entries[i].entry_id
fed.find(1) # <- FAILS HERE!!!
#if not fed.Find(1) then
if fed.url.include? "twitter.com" then
fed.feed_service="twitter"
elsif fed.url.include? "github.com" then
fed.feed_service="github"
elsif fed.url.include? "flickr.com" then
fed.feed_service="flickr"
elsif fed.url.include? "youtube.com" then
fed.feed_service="youtube"
elsif fed.feed_id.include? "google.com" then
fed.feed_service="reader"
else
fed.feed_service = "reader"
end
fed.save
#end
end
end
puts "Done creating feed"
puts "done."
end
I've got a rakefile with a cron job in it that accesses one of my models and then does a search on my database. I'm including my environment and see to be able to create my object, but when I try to run the "find" method, I get this error:
rake aborted!
undefined method `find' for #<Feeder:0x103611df0>
/Library/Ruby/Gems/1.8/gems/activemodel-3.0.4/lib/active_model/attribute_methods.rb:364:in `method_missing'
/Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/attribute_methods.rb:46:in `method_missing'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:28
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:20:in `each'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:20
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:18:in `each'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:18
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
Here is the code:
require 'rails/all'
require 'rake/clean'
require File.expand_path('../config/application', __FILE__)
require 'rubygems'
require 'sqlite3'
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
puts "Running cron at #{Time.now.strftime('%Y/%m/%d %H:%M:%S')}..."
require 'app/models/feeder'
num_feeds = 10
feedurls = ["http://twitter.com/statuses/user_timeline/2205491.rss","http://github.com/joshkerr.atom","http://gdata.youtube.com/feeds/base/users/joshkerr/uploads","http://api.flickr.com/services/feeds/photos_public.gne?id=95219360@N00&lang=en-us&format=rss_200","http://www.google.com/reader/public/atom/user%2F00755727578217480286%2Fstate%2Fcom.google%2Fbroadcast"]
feedurls.each do |feed|
rawfeed = Feedzirra::Feed.fetch_and_parse(feed)
for i in 0..num_feeds do
# Database call
fed = Feeder.new
fed.title = rawfeed.entries[i].title
fed.published = rawfeed.entries[i].published
fed.url = rawfeed.entries[i].url
fed.feed_id = rawfeed.entries[i].entry_id
fed.find(1) # <- FAILS HERE!!!
#if not fed.Find(1) then
if fed.url.include? "twitter.com" then
fed.feed_service="twitter"
elsif fed.url.include? "github.com" then
fed.feed_service="github"
elsif fed.url.include? "flickr.com" then
fed.feed_service="flickr"
elsif fed.url.include? "youtube.com" then
fed.feed_service="youtube"
elsif fed.feed_id.include? "google.com" then
fed.feed_service="reader"
else
fed.feed_service = "reader"
end
fed.save
#end
end
end
puts "Done creating feed"
puts "done."
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
find
是ActiveRecord
类上的方法,并且未在ActiveRecord
对象的实例上定义。因此,要查找 ID 为 1 的Feeder
,您需要调用Feeder.find(1)
。我注意到的另一件事是:
task :cron => 行:environment
表示环境已被加载 - 这包括rails/all
、rubygems
(您使用的是 Rails 3,它使用 Bundler),sqlite3
(假设它位于您的Gemfile
中,并且您已运行bundle install
来生成Gemfile.lock
,并且尝试删除所有requires
行并查看您的 rake 任务是否仍然执行。find
is a method on anActiveRecord
class, and is not defined on an instance of anActiveRecord
object. So, to find theFeeder
with ID of 1, you'd callFeeder.find(1)
.The other thing I noticed is this: the line
task :cron => :environment
means that the environment is already being loaded--this includesrails/all
,rubygems
(you're using Rails 3, which uses Bundler),sqlite3
(assuming it's in yourGemfile
and you've runbundle install
to generate yourGemfile.lock
, and all your models. Try removing all therequires
lines and see if your rake task still executes.