【问题标题】:rake task: Undefined method for Classrake 任务:类的未定义方法
【发布时间】:2013-01-14 23:35:23
【问题描述】:

我已经向我的 RakeFile 添加了一个新任务(我知道这样做的新方法是将您的任务添加到 lib/tasks,但其他任务在 RakeFile 中,我还不想重构。)任务我添加的访问模型(可能不是因为模型名称不在错误中)但不会访问它的方法。

rake aborted!
undefined method `transcode' for #<Class:0x10700e878>

我在 RakeFile 中的任务非常简单;

namespace :casta do
  desc "Transcode user videos from S3"
  task :transcode => :environment do
    ProfileVideo.transcode
  end
end

我的模型非常简单;

class ProfileVideo < ActiveRecord::Base

  belongs_to :application_form

  def transcode
    puts "Transcoding"
  end

end

我的其他 RakeFile 任务使用脚本/运行程序,它们工作得非常好。

导轨 2.3.14
rake 0.8.7(我在 0.9.2 虽然降级为测试)

希望得到一些见解,谢谢。

【问题讨论】:

    标签: ruby-on-rails rake rake-task rakefile


    【解决方案1】:

    您将 transcode 作为类方法调用,因此将 transcode 方法更改为:

      def self.transcode
        puts "Transcoding"
      end
    

    或者更可能是您想要的:您可以创建 ProfileVideo 的实例并在其上调用 transcode,然后保持 transcode 方法不变:

      task :transcode => :environment do
        pv = ProfileVideo.new(attributes)
        pv.transcode
      end
    

    【讨论】:

    • 射门得分!完美的!非常感谢!
    • 哦,我真的很喜欢第二个选项,完美。
    猜你喜欢
    • 2011-07-14
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 2015-02-22
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多