Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何为该自定义方法创建延迟作业?_Ruby On Rails_Postgresql_Delayed Job - Fatal编程技术网

Ruby on rails 如何为该自定义方法创建延迟作业?

Ruby on rails 如何为该自定义方法创建延迟作业?,ruby-on-rails,postgresql,delayed-job,Ruby On Rails,Postgresql,Delayed Job,这是我的教学模式: #encoding: utf-8 class Lesson < ActiveRecord::Base attr_accessible :content, :title, :parsed_content, :html_content, :user_id serialize :parsed_content, Array serialize :html_content, Array serialize :pinyin_content, Ar

这是我的教学模式:

#encoding: utf-8

class Lesson < ActiveRecord::Base
    attr_accessible :content, :title, :parsed_content, :html_content, :user_id

    serialize :parsed_content, Array
    serialize :html_content, Array
    serialize :pinyin_content, Array
    serialize :defined_content, Array
    serialize :literal_content, Array

    validates :title, :presence => true
    validates :content, :presence => true

    belongs_to :user

    before_update do |lesson|
        lesson.makesandwich
    end

    before_save do |lesson|
        lesson.delay.makesandwich
    end

    def makesandwich

        require 'rmmseg'
                                                          #require 'to_lang'
        require 'bing_translator'
        require 'ruby-pinyin'

        self.parsed_content = []

        RMMSeg::Dictionary.load_dictionaries

        content               = self.content
        paragraphs            = content.split(/\r\n\r\n/) #convert to array of paragraphs
        self.parsed_content = paragraphs
        paragraphs.each_with_index do |text, ti|

            text = text.gsub("。", "^^.")
            text = text.gsub("?", "~~?")
            text = text.gsub("!", "||!")
            text = text.gsub(":", ":")  #fix missing colons

            text = text.split(/[.?!]/u) #convert to an array
            text.each do |s|
                s.gsub!("^^", "。")
                s.gsub!("~~", "?")
                s.gsub!("||", "!")
                #s.gsub!("———————————",":")
            end

            text.each_with_index do |val, index|
                algor     = RMMSeg::Algorithm.new(text[index])
                splittext = []
                loop do
                    tok = algor.next_token
                    break if tok.nil?
                    tex = tok.text.force_encoding('UTF-8')
                    splittext << tex
                    text[index] = splittext
                end
                paragraphs[ti] = text
            end
        end
        bing                   = BingTranslator.new(BING_API)
        self.parsed_content  = paragraphs
        textarray              = Marshal.load(Marshal.dump(paragraphs))
        self.defined_content = Marshal.load(Marshal.dump(paragraphs))
        self.literal_content = Marshal.load(Marshal.dump(paragraphs))
        self.pinyin_content  = Marshal.load(Marshal.dump(paragraphs))
        textarray.each_with_index do |paragraph, pi|
            paragraph.each_with_index do |sentence, si|
                sentence.each_with_index do |word, wi|
                    if DictionaryEntry.find_by_simplified(word) != nil
                        self.defined_content[pi][si][wi] = DictionaryEntry.find_by_simplified(word).definition
                        #self.literal_content is down below
                        self.pinyin_content[pi][si][wi]  = DictionaryEntry.find_by_simplified(word).pinyin
                    else
                        self.defined_content[pi][si][wi] = bing.translate(word, :from => 'zh-CHS', :to => 'en')
                        #self.defined_content[pi][si][wi] = word
                        #self.literal_content is down below
                        if PinYin.of_string(word, true).length > 1 #for punctuation
                            self.pinyin_content[pi][si][wi] = PinYin.of_string(word, true).join(" ").downcase
                        else
                            self.pinyin_content[pi][si][wi] = word
                        end
                    end
                end
            end
        end

        #Literal
        literalarray = Marshal.load(Marshal.dump(paragraphs))
        literalarray.each_with_index do |paragraph, pi|
            paragraph.each_with_index do |sentence, si| #iterate array of sentence
                literalarray[pi][si] = []
                sentence.each_with_index do |word, wi| #iterate sentence's array of words
                    entrytobesliced = DictionaryEntry.find_by_simplified(word)
                    slicedentry     = []

                    if entrytobesliced == nil
                        if word.length > 1 && word !~ /\w/ #/^\s*\w\d+\s*$/ #number regex  #for cases where there is no DictionaryEntry
                            split     = []
                            wordarray = word.split("").each_with_index() do |ws, wsi|
                                split << [DictionaryEntry.find_by_simplified(ws).definition]
                            end
                            literalarray[pi][si] << split
                        else
                            literalarray[pi][si] << [word] #in case none of the above work
                        end
                    else
                        entrytobesliced.simplified.each_char do |w|
                            singlechar = DictionaryEntry.find_by_simplified(w)
                            slicedentry << singlechar.definition.split("\", \"")
                        end
                        literalarray[pi][si] << slicedentry
                    end
                    self.literal_content = literalarray #slicedentry #literalarray
                end
            end


        end
    end
end

我认为您会遇到以下错误:

在记录被持久化之前,无法为其创建作业


因为您的
课程
实例在保存之前不会有
id
,并且没有
id
,所以DJ无法知道应该使用哪个实例。因此,您必须在保存后使用
,以便您的
课程
具有
id
,并且可以唯一识别。但是,延迟作业中的更新不会被保存,因为没有要求保存它们。您只需添加一个
self.save
self.save即可绕过此问题makesandwich

的末尾调用code>将
self.save
放入其中似乎会导致一个无限循环,因为它在末尾再次执行回调。有什么东西可以保存它但不能运行回调吗?@webmagnetics:是的,当然可以,发烧和编程不能很好地结合在一起。您应该能够添加一个
:if=>:new\u record?
:if=>:content\u changed?
来解决这个问题。很抱歉,您发烧了。请你说得更清楚些。我还没有看到带有
if
语句的那种语法。不是
if
语句,而是
:如果
以回调为条件,指南应该解释一下:我将
在保存后更改为
在创建后。这样,
self.save
行就不会在无限循环中运行回调。谢谢你的帮助。
    before_save do |lesson|
        lesson.makesandwich #no delay
    end