书中的ActiveRecord :: StatementInvalid #index
TypeError:无法将Array强制转换为字符串:INSERT INTO“amazonitems” (“asin”,“author”,“created_at”,“detailpageurl”,“isbn”, “l_image_url”,“m_image_url”,“制造商”,“product_group”, “publication_date”,“s_image_url”,“title”,“updated_at”)VALUES(?, ?,?,?,?,?,?,?,?,?,?,?,?)
application_helper
module ApplicationHelper
def get_amazon_item_data(asin)
rec = Amazonitem.where(asin: "#{asin}").first
if !rec or rec.updated_at < DateTime.now - 0.5
unless rec
rec = Amazonitem.new() unless rec
rec.asin = asin
end
amazon_res = Amazon::Ecs.item_lookup(asin, response_group: 'Medium')
if amazon_res and amazon_res.first_item
item = amazon_res.first_item
element = item.get_element('ItemAttributes')
rec.detailpageurl = item.get('DetailPageURL'),
rec.isbn = element.get("ISBN"),
rec.title = element.get("Title"),
rec.author = element.get_array("Author").join(","),
rec.manufacturer = element.get("Manufacturer"),
rec.product_group = element.get("ProductGroup"),
rec.publication_date = element.get("PublicationDate"),
rec.s_image_url = item.get("SmallImage/URL"),
rec.m_image_url = item.get("MediumImage/URL"),
rec.l_image_url = item.get("LargeImage/URL")
else
rec.detailpageurl = nil
end
rec.updated_at = DateTime.now
rec.save
end
return rec
end
end
我无法保存到sqlite db中。帮助我。
答案 0 :(得分:2)
rec
个属性之一是数组。尝试添加(暂时):
raise Hash[rec.attributes.select {|_,v| v.is_a? Array}].inspect
在保存并更正给定属性分配之前 - db期待一个字符串。
另请勿触摸updated_at
列 - 它会自动更新。
答案 1 :(得分:2)
使用to_s来防止此类错误。例如,在我的应用程序中,由于此代码,我收到错误 -
Urllist.create(
:url => @abc,
:success => @success,
:failed => @failed,
:failedemail => @faillikearray
)
此处@faillikearray
是一个数组。我的问题已经解决了 -
Urllist.create(
:url => @abc,
:success => @success,
:failed => @failed,
:failedemail => @faillikearray.to_s
)