Hey, trying to add some products via a Rake task, ...
# support
w
Hey, trying to add some products via a Rake task, but can anyone tell what I'm doing wrong?
Copy code
[...]

created_product = Spree::Product.find_or_create_by(
  name: name,
  price: price,
  description: description,
  available_on: Time.current,
  shipping_category: shipping_category,
  tax_category: tax_category,
  taxon_ids: [taxon_ids]
)
puts created_product.name

options = $browser.at_css("#productOverview_feature_div table")

values = []

options.css("tr").each_with_index do |row, index|
  type = row.at_css("td:nth-of-type(1) span").text.strip
  value = row.at_css("td:nth-of-type(2) span").text.strip

  created_type = Spree::OptionType.find_or_create_by(
    name: type,
    presentation: type
  )
  # created_product.option_types << created_type.name
  # created_product.save
  # => ActiveRecord::AssociationTypeMismatch: Spree::OptionType(#74440) expected, got "Brand" which is an instance of String(#5020)

  created_value = Spree::OptionValue.find_or_create_by(
    name: value,
    presentation: value,
    option_type: created_type.name
  )

  puts "#{ created_type.name }: #{ created_value.name }"

  values << created_value.name
end

created_product.variants.create(
  option_values: [values],
  sku: rand(100000)
)
created_product.save!
# => ActiveRecord::RecordInvalid: Validation failed: Option types is invalid, Variants is invalid, Variants including master is invalid
m
You don´t want to add the option type's name as the product's option, type, you want to add the option type itself. Similar with the option value. Same mistake when creating the option values, you want the reference to the option type be the full ActiveRecord model, not just the name.
w
Gotcha, thanks one sec please...
m
in line 128, you need to to remove the square brackets
w
Pardon the disconnect!
I'm glad it's just square brackets and that I don't have to rewrite the entire script.
Very nice, somehow this error feels like the last error 🙏
Copy code
Ferrum::NodeNotFoundError: No node with given id found
/home/dev/wilfred/lib/tasks/amazon.rake:86:in `block (2 levels) in import_amazon'
/home/dev/wilfred/lib/tasks/amazon.rake:83:in `each'
/home/dev/wilfred/lib/tasks/amazon.rake:83:in `block in import_amazon'
/home/dev/wilfred/lib/tasks/amazon.rake:75:in `each'
/home/dev/wilfred/lib/tasks/amazon.rake:75:in `import_amazon'
/home/dev/wilfred/lib/tasks/amazon.rake:231:in `block (3 levels) in <main>'
/home/dev/wilfred/lib/tasks/amazon.rake:230:in `each'
/home/dev/wilfred/lib/tasks/amazon.rake:230:in `block (2 levels) in <main>'
Tasks: TOP => scrape:amazon
(See full trace by running task with --trace)
I also added some random user-agents: https://pastebin.com/crPYvE7e I don't yet qualify for the Amazon Product Advertising API (I need an insane amount of traffic for that) so it's either scraping or manually copy/pasting.
Any idea what node or id it's referring to?
Also would you have done anything different if it was your personal scraper? 👀