Importing records into custom engine via CSV?

12 views
Skip to first unread message

ni...@tinygoatstudio.com

unread,
Apr 10, 2014, 9:28:29 AM4/10/14
to refine...@googlegroups.com
Hi all:

I've built a custom "products" engine. I'd like to import a long list of products via a CSV file, but I frankly have no idea how. This seems fairly straightforward in plain vanilla Rails, but it just isn't working at all inside my custom engine. I've built the following rake task to handle the import:

require 'csv'

namespace :refinery do
  namespace :products do

    # call this task by running: rake refinery:products:import_csv
    desc "Import products via CSV"
    task :import_csv => :environment do

      CSV.foreach('db/product.refinery_products.csv') do |row|

        puts "Counting headers in CSV file..."

        product_id              = row[0]
        model_number            = row[1]
        category                = row[2]
        subcategory             = row[3]
        description             = row[4]
        meta_heading            = row[5]
        logo                    = row[6]
        lo_res_image            = row[7]
        hi_res_image            = row[8]
        main_description        = row[9]
        feature_indications     = row[10]
        certification           = row[11]

        puts "Adding products to database..."

        Refinery::Products.create(
          product_id: product_id, 
          model_number: model_number, 
          category: category, 
          subcategory: subcategory, 
          description: description,
          meta_heading: meta_heading,
          logo: logo,
          lo_res_image: lo_res_image,
          hi_res_image: hi_res_image,
          main_description: main_description,
          feature_indications: feature_indications,
          certification: certification
        )

        puts "Finished"

      end
    end
  end
end

When I run it, I get the error "undefined method `create!' for Refinery::Products:Module". So, what methods do I have access to in the models that are created when I make a custom engine—and how can I make this work?

Thanks all!

Philip Arndt

unread,
Apr 10, 2014, 9:29:26 AM4/10/14
to refine...@googlegroups.com
Instead of Refinery::Products.create you want to use Refinery::Products::Product.create otherwise you're calling create on the Products namespace and not on the Product model. :)
Reply all
Reply to author
Forward
0 new messages