【发布时间】:2015-06-06 15:11:52
【问题描述】:
尝试使用delayed_job_active_record 将一些东西发送到后台,但是我为什么不能使用any??当事情没有发送到后台时工作正常。
undefined method `any?' for #<Delayed::Backend::ActiveRecord::Job:0x000000044b1348>
index.html.erb
<% if @products.any? %>
<div class="products">
<% @products.each do |product| %>
<%= product.name %>
<% end %>
</div>
<% else %>
<div class="products processing">
<!-- Try again later -->
</div>
<% end %>
main_controller.rb
class MainController < ApplicationController
def index
# Delay fetching
@products = Affiliate.delay.fetch
end
end
affiliate.rb
require "rest_client"
class Affiliate < ActiveRecord::Base
def self.fetch
response = RestClient::Request.execute(
:method => :get,
:url => "http://api.shopstyle.com/api/v2/products?pid=uid7849-6112293-28&fts=women&offset=0&limit=10"
)
@products = JSON.parse(response)["products"].map do |product|
product = OpenStruct.new(product)
affiliate = Affiliate.find_or_create_by(:name => product.name, :url => product.url)
affiliate.save
end
end
end
【问题讨论】:
-
@items 是什么以及它从何而来?
标签: ruby-on-rails activerecord delayed-job