即使我包含环境,object.find也会在rakefile中失败

时间:2011-02-24 06:47:37

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

我有一个带有cron作业的rakefile,可以访问我的一个模型然后搜索我的数据库。我包含了我的环境并且看到能够创建我的对象,但是当我尝试运行“find”方法时,我收到了这个错误:

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

1 个答案:

答案 0 :(得分:1)

findActiveRecord类上的方法,并未在ActiveRecord对象的实例上定义。因此,要查找ID为1的Feeder,您需要拨打Feeder.find(1)

我注意到的另一件事是:行task :cron => :environment表示已加载环境 - 包括rails/allrubygems(您使用的是Rails 3,其中使用Bundler),sqlite3(假设它在你的Gemfile中,并且你运行bundle install来生成Gemfile.lock以及所有模型。尝试删除所有{{1}行,看看你的rake任务是否仍然执行。