【问题标题】:Resque can't update objects - railsResque 无法更新对象 - rails
【发布时间】:2013-09-01 06:47:45
【问题描述】:

我正在尝试做一个简单的导入后台作业来使用 resque 导入一些 csv 信息。

作业正在运行,但是一旦我编辑了一个对象,它似乎就被毁了或其他什么......

一旦该作业运行,它会找到具有第一个 lin 的用户...然后在 @user.name 处没有设置,然后无法保存。我可以在我的控制台中运行这段代码,它工作得很好,只是在 resque 中被破坏了。是否存在不能使用对象或使用 resque 写入数据库的限制?到目前为止浪费了 6 个小时尝试了无数的事情......请帮忙。

def self.perform(chunk)
    chunk.each do |i|
        @user = User.where(:email => i[:email].to_s).first_or_initialize
        @user.name = i[:name].to_s
        if @user.save
            puts @user.email
            entity = Entity.find_by_email_domain(@user.email)
            eur = EntityUser.where(:entity_id => entity.id, :user_id => @user.id).first_or_initialize
            if eur.save
                puts "Start: BLARGITY End"
                topic = Topic.where(:number => i[:course_number].to_s).first_or_initialize
                eu = TopicUser.where(:topic_id => topic.id, :user_id => user.id, :role_i => 1).first_or_initialize
            else
                eu = TopicUser.where(:topic_id => topic.id, :user_id => user.id).first_or_initialize
            end
        eu.save
        end
    end

 end

end

我只是尝试像这样进行查找,您现在可以看到错误..

NoMethodError: nil:NilClass 的未定义方法 `name='

    @user = User.find_by_email(i[:email])
    puts "sdfdsf"
    @user.name = i[:name]
    puts @user.name

【问题讨论】:

    标签: ruby-on-rails resque


    【解决方案1】:

    我使用 smarter_csv 发送“块”,这是一个哈希。我猜redis序列化一个hash的时候,需要先符号化,然后再开始使用。

    chunk.each do |i|
            i.symbolize_keys!
            @user = User.find_by_email(i[:email])
            @user.name = i[:name]
    

    符号化后起作用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 2012-11-11
      • 2018-08-15
      相关资源
      最近更新 更多