尝试使用Nokogiri运行rake任务时出现此错误。
这是代码。
namespace :tops do
desc "Get Tops Description"
task get_description: :environment do
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://ceratoboutique.com/collections/tops"))
price = Array.new
items = Array.new
brands = Array.new
sale_price = Array.new
image = Array.new
item_url = Array.new
price = doc.xpath("//del").collect {|node| node.text.strip}
items = doc.xpath("//div/a/h4").collect {|node| node.text.strip}
brands = doc.xpath("//span[contains(@class,'vendor')]").collect {|node| node.text.strip}
sale_price = doc.xpath("//span[contains(@class, 'price')]/text()").collect {|node| node.text.strip}
image = doc.xpath("/div/a/img/@src").collect {|node| node.text.strip}
item_url = doc.css('div.details a').map { |link| link['href'] }.collect
price.each do |prices|
Tops.create(description: price)
end
end
end
这是完整的错误。
耙子流产了! TypeError:无法将Array转换为字符串 /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/quoting.rb:76:intype_cast' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in
type_cast' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:inblock in exec_query' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in
map' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:inexec_query' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in
exec_insert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:95:ininsert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in
插入” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/relation.rb:64:ininsert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:502:in
_ create_record” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:87:in {_ 1}}阻止_create_record' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:in_create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in
_ create_record” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/timestamp.rb:57:inrun_callbacks' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in
create_or_update” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:in_create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:482:in
run_callbacks' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:inblock in create_or_update' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:in
保存” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/validations.rb:51:increate_or_update' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:103:in
保存” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:insave' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:21:in
阻止了with_transaction_returning_status' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:inblock (2 levels) in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:329:in
within_new_transaction” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:inblock in transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in
交易” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:326:intransaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:208:in
阻止保存' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:283:inwith_transaction_returning_status' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:in
save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:34:in 'rollback_active_record_state!' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:267:in
阻止(3级)' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:increate' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:24:in
块(2 水平)' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:ineach' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:in
阻止执行' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:incall' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in
执行” /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:179:ineach' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in
mon_synchronize” /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:172:inblock in invoke_with_call_chain' /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in
调用” /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:150:in top_level中invoke_with_call_chain' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:165:in
阻止(2个级别) /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in top_level中的invoke_task' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in
阻止' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:115:ineach' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in
TOP_LEVEL” /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:78:inrun_with_threads' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:100:in
standard_exception_handling” /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:75:inblock in run' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:176:in
'/ usr / bin / rake:23:run' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/bin/rake:33:in
'
编辑:TOPS TABLE SCHEMA
load' /usr/bin/rake:23:in
答案 0 :(得分:7)
似乎表description
中的tops
字段为“varchar”或string
,但您尝试将数组存储在其中。
在尝试存储之前,您需要将description
字段的数据类型更改为text
,然后再将serialize
更改为该值。
生成新的迁移文件并运行它,在您的终端上:
rails g migration change_desc_in_my_tops
在使用Rails 4时,请在迁移文件中添加以下代码:
class ChangeDescInTops < ActiveRecord::Migration
def up
change_column :tops, :description, :text
end
def down
change_column :tops, :description, :string
end
end
现在运行迁移:
rake db:migrate
将您的tops
模型更新为:
class Tops < ActiveModel::Base
serialize :description, Array
...
...
end
现在尝试运行你的rake任务。希望,它会帮助你。
注意:您应该使用单数型号名称。您的Tops
模型可能是Top
。它避免了使用关联时的混淆,它遵循惯例。
答案 1 :(得分:2)
你把price
设置为一个数组(而不是称之为prices
)让你很困惑,这可能是你输入错误的一个因素
price.each do |prices|
Tops.create(description: price)
end
您正在分配正在迭代的数组,而不是正在生成的单个元素。