Rails user experience vs file structure with carrierwave and nested forms - Need input on design decisions

I have 2 models, product and a photos model as follows:

product.rb

class Product < ActiveRecord::Base
  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos, allow_destroy: true
end

photo.rb

class Photo < ActiveRecord::Base
  belongs_to :product
  validates :album, presence: true
  mount_uploader :image, PhotoUploader
end

I'm using carrierwave & s3 for file uploads.

I can nest the forms so that users can upload photos at the same time that they create the product. In this scenario, the photo models are saved before the product model is saved, which leads to a photo file path of something like:

uploads/photos/images/photo_id

But the file structure would make more sense if it was something like:

uploads/product/id/photos/

However, product.id is nil at the time of photo creation.

With that in mind, a few questions:

  • Does the file structure really matter that much or am I over thinking it?
  • Should I do something instead where the product is saved first with basic info and then the files are attached on a separate form?
  • Is there something I'm overlooking like a way to trigger carrierwave to move the files after the product model is saved?
  • From a UI standpoint is it bad practice to have users go thru a multi-step process (in this case for creating a product)?

1 Answer

  1. Justin- Reply

    2019-11-15

    My solution to have a path such as uploads/products/id/photos/ is to configure the PhotoUploader as follows:

      def store_dir
        "uploads/products/#{model.product_id}/images"
      end
    

    Just be careful that there are no validations that would cause product_id to be non-existant at the time the store_dir method is called.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>