Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:00:33+00:00 2026-06-17T05:00:33+00:00

Here is my Lesson model: #encoding: utf-8 class Lesson < ActiveRecord::Base attr_accessible :content, :title,

  • 0

Here is my Lesson model:

#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

When I try to create a new lesson it errors like this: Jobs cannot be created for records before they've been persisted

But if I change it to after_save instead of before_save then I can see the work run, but it doesn’t update the serialized arrays in the database.

Can someone please help me implement delayed_jobs for this? It was working when I had:

    before_save do |lesson|
        lesson.makesandwich #no delay
    end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:00:34+00:00Added an answer on June 17, 2026 at 5:00 am

    I think you’re getting these errors:

    Jobs cannot be created for records before they’ve been persisted

    because your Lesson instances won’t have an id until they’ve been saved and without an id, DJ has no way to know which instance it should be working with. So you have to use an after_save so that your Lesson has an id and can be uniquely identified. But then your updates from the delayed job won’t be saved because, well, nothing asks for them to be saved. You should be able to get around that simply by adding a self.save or self.save! call at the end of makesandwich.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here is the jsfiddle code I found this problem during a CodeAcademy lesson. Same
The tutorial I'm following ( http://www.bluerosegames.com/xna101/post/Lesson-9-Improving-the-BouncingBall-Class-Using-Properties.aspx ) private Vector2 _position; public Vector2 Position {
I am learning Go and am working on this lesson from the GoTours. Here's
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my work environment: Eclipse Juno as IDE with maven2 plugin on it
Here is what I am currently doing. PHP echo's out the recent post in
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.