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

发布于 2024-10-19 03:23:22 字数 3964 浏览 0 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-10-26 03:23:22

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

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

find is a method on an ActiveRecord class, and is not defined on an instance of an ActiveRecord object. So, to find the Feeder with ID of 1, you'd call Feeder.find(1).

The other thing I noticed is this: the line task :cron => :environment means that the environment is already being loaded--this includes rails/all, rubygems (you're using Rails 3, which uses Bundler), sqlite3 (assuming it's in your Gemfile and you've run bundle install to generate your Gemfile.lock, and all your models. Try removing all the requires lines and see if your rake task still executes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文