Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make a delayed job for this custom method?
    primarykey
    data
    text
    <p>Here is my Lesson model:</p> <pre><code>#encoding: utf-8 class Lesson &lt; 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 =&gt; true validates :content, :presence =&gt; 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 &lt;&lt; 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 =&gt; 'zh-CHS', :to =&gt; 'en') #self.defined_content[pi][si][wi] = word #self.literal_content is down below if PinYin.of_string(word, true).length &gt; 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 &gt; 1 &amp;&amp; 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 &lt;&lt; [DictionaryEntry.find_by_simplified(ws).definition] end literalarray[pi][si] &lt;&lt; split else literalarray[pi][si] &lt;&lt; [word] #in case none of the above work end else entrytobesliced.simplified.each_char do |w| singlechar = DictionaryEntry.find_by_simplified(w) slicedentry &lt;&lt; singlechar.definition.split("\", \"") end literalarray[pi][si] &lt;&lt; slicedentry end self.literal_content = literalarray #slicedentry #literalarray end end end end end </code></pre> <p>When I try to create a new lesson it errors like this: <code>Jobs cannot be created for records before they've been persisted</code></p> <p>But if I change it to <code>after_save</code> instead of <code>before_save</code> then I can see the work run, but it doesn't update the serialized arrays in the database.</p> <p>Can someone please help me implement delayed_jobs for this? It was working when I had:</p> <pre><code> before_save do |lesson| lesson.makesandwich #no delay end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload