由于ActiveRecord :: StatementInvalid,Rails delayed_job无法正常工作:SQLite3 :: SQLException:无法回滚

时间:2017-07-10 04:46:31

标签: ruby-on-rails ruby

ActiveRecord :: StatementInvalid:SQLite3 :: SQLException:无法回滚 - 没有事务处于活动状态:回滚事务

我试图在heroku中保存大量数据,但由于响应时间限制为30秒,我无法做到这一点,因此我决定使用delayed_job。 但是,因为我从未使用过delayed_job所以我不知道如何解决以下错误

错误

NoMethodError(未定义的方法`quizlet_save' for Word(id:integer,group_id:integer,user_id:integer,answer:string,question:string,title:string,image:string,created_at:datetime,updated_at:datetime ,answer_language:string,question_language:string,favorites_count:integer,quizlet_id:integer,check_id:string)):

   def index
@words = params[:tag].present?? Word.tagged_with(params[:tag]) : Word.all
@words =@words.all.includes(:tags)
@groups=Group.all
@search=@words.ransack(params[:q])
@search_result = @search.result #この行を修正
@search_result = @search_result.uniq
respond_to do |format|
  format.html
  format.json { render json: @search_result}
end
quizlet

  def quizlet
      @response=[]
               response = Unirest.get "https://api.quizlet.com/2.0/search/sets/?client_id=7JH38T27zM&whitespace=1",
                       parameters:{ whitespace: true,q: "Duo3.0",term:100,sort:"most_studied"}
      res=response.body

      quizlet_ids=[]
        res["sets"].each do|res|
           quizlet_ids<<res["id"]
       end


      quizlet_words = Unirest.get "https://api.quizlet.com/2.0/sets/?client_id=7JH38T27zM&whitespace=1",
                            parameters:{ whitespace: true,set_ids:"#{quizlet_ids}"}

   quizlet_words.body.each do|words|
     words["terms"].each do|word|
      response=word
    response["quizlet_id"]=words["id"]
    response["check_id"]=word["id"]
    response["question_language"]= words["lang_terms"]
    response["answer_language"]  = words["lang_definitions"]
    @response<<response
    @response.each do|response|
      response
      Word.delay.quizlet_save(response["quizlet_id"],response["check_id"],response["term"],response["definition"],response["question_language"],response["answer_language"])
    end
  end
end

模型

def quizlet_save(quizlet_id,check_id,question,answer,question_language,answer_language)
     unless Word.find_by(check_id:check_id)&&Word.find_by(quizlet_id:quizlet_id)
     Word.create(quizlet_id:quizlet_id,check_id:check_id,title:"Quizlet",question:question,answer:answer,user_id:1,tag_list:"English,duo3.0,Vocabulary",
              question_language:question_language,answer_language:answer_language)
    end
 end

  def quizlet_create_groups(quizlet_id,check_id)
   word=Word.find_by(check_id:check_id)
   unless word == nil
    checking_word=Word.find_by(quizlet_id:quizlet_id)
    group=Group.find_by(id: checking_word.group_id)
    if group==nil
       created_group=Group.new(user_id:1,title:"Quizlet",explanation:"Quizlet Duo3.0")
       created_group=created_group.save(validate: false)&&created_group
       word.update_attribute(:group_id,created_group.id)
     else
    word.update_attribute(:group_id,group.id)
    end
   end
 end

环境

  • ruby​​ 2.3.1
  • ruby​​ on rails 4.2.7
  • delayed_job_active_record(4.1.2)

1 个答案:

答案 0 :(得分:0)

我可以在delayed_job的Github问题上找到解决方案.referner_job存储库的所有者告诉我,除了SQLite中的sucker_punch之外,你不能使用任何异步宝石。谢谢你回答。