i am import feed and have two models product and images from feed hot i am try to firstly erase all associed fields but line record.Images.clear failed, can anyone give me hint
import code
def monditrade
require "nokogiri"
require "open-uri"
doc = Nokogiri::XML(open("http://ift.tt/1lOiVqd"))
doc.css('product').each do |node|
children = node.children
record = Product.where("supplier_link" => children.css('link').inner_text).first_or_initialize
record.name=children.at("name").inner_text
record.delivery=children.at('delivery_date').inner_text
record.desc=children.at('description').inner_text
record.desc_long=children.at('description2').inner_text
record.supplier_name="monditrade"
record.supplier_code=children.at("code").inner_text
ean=children.at('ean')
if ean
record.ean=ean.inner_text
end
record.manufacturer=children.at('manufacturer').inner_text
record.supplier_code=children.at('code').inner_text
record.supplier_category=children.at('category').inner_text
record.Images.clear
children.css('img').each do |node|
image = Image.where("url" => node.inner_text.squish,"product_id"=> record.id).first_or_initialize
image.save
end
record.save
end
end
product.rb
class Product < ActiveRecord::Base
has_many :Image
end
image.rb
class Image < ActiveRecord::Base
belongs_to :Product
end
and in controller i am parse some xml feeed
example feeed
No comments:
Post a Comment