Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to import nested attributes containing remote urls? #687

Open
imtiaz-emu opened this issue Jul 22, 2020 · 4 comments
Open

Comments

@imtiaz-emu
Copy link

imtiaz-emu commented Jul 22, 2020

I'm trying to create records from a csv file. Each row of the CSV file will be a new record.

def import
  ads = []
  CSV.foreach(file.path, headers: true, encoding: encoding) do |row|
      ad = Ad.new
      ad.title = row['title'] || ''
      ad.description = row['description'] || ''
      
      # Ad has_one: variant
      ad.variant_attributes = initialize_ad_variant(row).attributes
      
      ads << ad
  end
  Ad.import(ads, recursive: true) # I'm using postgres  
end

def initialize_ad_variant(row)
     variant = Variant.new
     # inside CSV I have a column called 'custom_image_url', which contains an image url from where we need to save the image. 
     # 'custom_image' is the field name in variant table. I'm using carrierwave.
     variant.remote_custom_image_url = row['custom_image_url']
     variant.custom_text = row['custom_text']
     return variant
end

The above code inserts Ads with associated variants. But seems like the images are not uploaded inside the variant table. Only the custom_text is inserted.
Is there any way to bulk upload nested attributes like this; where the images will be uploaded from remote url?

@jkowens
Copy link
Collaborator

jkowens commented Jul 24, 2020

I don't think so at this time. Are you using ActiveStorage? This might be a duplicate of issue #661.

@imtiaz-emu
Copy link
Author

I don't think so at this time. Are you using ActiveStorage? This might be a duplicate of issue #661.

Got it. No, I'm not using ActiveStorage.

@jkowens
Copy link
Collaborator

jkowens commented Jul 24, 2020

Without knowing more of the internals of how the remote images are loading it's hard to give any suggestions. I would say try to find out if there is some type of call you make on each variant to manually load the image before importing.

@BrandonTeixeiraStream
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants