Search code examples
ruby-on-railsimagemagickpapercliprake-task

Rails rake file and uploading multiple image assets


I'm using a rake file to upload existing content on a site (populating from CSV file and folder of images). This content consists of listings, which have various details, one uploaded "image" and up to 5 additional uploaded "assets" (photos).

I have a listing model that has one image, as well as an assets model, where up to 5 other images/assets live. Assets belongs to a listing, and a listing has many assets.

When creating a new listing from the web form, all works properly, and my controller looks like:

  def new
    @listing = current_user.listings.build
    5.times { @listing.assets.build}
  end

Images/assets are handled with paperclip and imagemagick

The rake file is working for all components except attaching assets(photos). I am able to upload the single image that's attached to the same model with:

image = File.open(Rails.root.join('location/', 'image_name'))

However I can't even upload a single asset with:

asset = File.open(Rails.root.join('location/', 'asset_name'))

My current rake file to upload content looks like:

     CSV.foreach(file, :headers => true) do |row|
        puts "[DEBUG] uploading new listing"
        image = File.open(Rails.root.join('sampleimages/', row[6])) #this is working properly to upload the single image that's attached to the listing model
        assets = File.open(Rails.root.join('sampleimages/', row[7])) #this is not working, even when I'm only trying to attach one of the possible five assets   
        User.last.listings.create!(assets: assets, image: image, listingname: row[20], provider_phone: row[13], provider_email: row[14], blah blah)
      end

I get the following error in my terminal when running the rake:populate:

Asset(#70364324499020) expected, got File(#70364281853460)

Ideas of where I'm going wrong? Effectively, I need to open 5 files, and save them as "assets".

Solution:

The issue was that I was trying to attach the image assets through the listing creations process:

User.last.listings.create!(attribute1: row[1], attribute2: row[2].....)

But since my asset model is separate from the listing model, I simply needed to use the following methodology:

asset1 = File.open(Rails.root.join('sampleimages/', row[7])) unless row[7].nil?
asset2 = File.open(Rails.root.join('sampleimages/', row[8])) unless row[8].nil?
asset3 = File.open(Rails.root.join('sampleimages/', row[9])) unless row[9].nil?
asset4 = File.open(Rails.root.join('sampleimages/', row[10])) unless row[10].nil?
asset5 = File.open(Rails.root.join('sampleimages/', row[11])) unless row[11].nil?
Asset.create!(asset: asset1, listing_id: User.last.listings.last.id) unless row[7].nil?
Asset.create!(asset: asset2, listing_id: User.last.listings.last.id) unless row[8].nil?
Asset.create!(asset: asset3, listing_id: User.last.listings.last.id) unless row[9].nil?
Asset.create!(asset: asset4, listing_id: User.last.listings.last.id) unless row[10].nil?
Asset.create!(asset: asset5, listing_id: User.last.listings.last.id) unless row[11].nil?

Hope that helps


Solution

  • Updated the question with my solution