shoujo_sm
shoujo_sm

Reputation: 3203

Delayed_job does nothing : TypeError: can't convert nil into String

Follow delayed_job from "https://github.com/collectiveidea/delayed_job". May I know why my delayed_job does nothing? I ran "rake jobs:work" ad got these errors

Error:

Company#update_count_without_delay failed with TypeError: can't convert nil into String - 2 failed attempts

Done:

  1. Added "gem 'delayed_job_active_record'" into gemfile
  2. In console: "rails generate delayed_job:active_record"
  3. In console: "rake db:migrate"

I followed this instruction:

If a method should always be run in the background, you can call #handle_asynchronously after the method declaration:

class Device
  def deliver
    # long running method
  end
  handle_asynchronously :deliver
end

device = Device.new
device.deliver

Model:

require 'json'
require 'net/http'
require 'rubygems'
require 'delayed_job'

class Company < ActiveRecord::Base
before_save :validate_fbid

scope :toplikes, order("count desc").limit(20)

attr_accessible :desc, :fbid, :name, :url, :count

url_regex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ 


validates :name,    :presence   => true

validates :url,     :presence   => true,
                :format     => { :with => url_regex },
                :uniqueness => { :case_sensitive => false }
validates :fbid,    :presence   => true
validates :desc,    :presence   => true

def update_count
    uri = URI("http://graph.facebook.com/" + fbid)
    data = Net::HTTP.get(uri)
    self.count = JSON.parse(data)['likes']
end

handle_asynchronously :update_count     
end 

Controller:

def create 
    company = Company.new(params[:company])

    if company.save 
        @message = "New company created."
        company.update_count
        redirect_to root_path
    else 
        @message = "Company create attempt failed. Please try again."
        redirect_to new_path 
    end             
  end 
  1. added "require 'delayed_job'" at top of model
  2. restart my server

Upvotes: 0

Views: 391

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

You have an error in your Company#update_count. It should be:

def update_count
  uri = URI("http://graph.facebook.com/" + fbid)
  data = Net::HTTP.get(uri)
  # here you should use your local variable that you set in previous line
  # instead of unset instance variable:
  update_attribute(:count, JSON.parse(data)['count'])
end

Upvotes: 1

Related Questions