Can someone please assist with regards to CarriveWave’s store_dir.
How does one have mulitple image models that stores files based on the associated belong_to model’s permalink?
# Garage model
class Garage < ActiveRecord:Base
attr_accessible: :avatar, :permalink, :car_image_attributes,
:motorcycle_image_attributes
has_many :car_image
has_many :motorcycle_image
mount_uploader :avatar, AvatarUploader
def set_permalink
self.permalink = permalink.parameterize
end
def to_param
permalink.parameterize
end
end
This is what my Image Models that links with CarrierWave
# Car Image model
CarImage < ActiveRecord:Base
belongs_to :garage
attr_accessible: :garage_id, :image
mount_uploader :car_image, CarUploader
end
# Motocycle Image model
MotocycleImage < ActiveRecord:Base
belongs_to :garage
attr_accessible: :garage_id, :image
mount_uploader :motorcycle_image, MotocycleUploader
end
This is what my CarrierWave uploaders look like.
# CarrierWave avatar uploader
avatar_uploader.rb
# This uploader directly relates to the Garage model table
# column avatar:string.
def store_dir
# This works for the avatar because it calls on the Garage permalink
# but it fails for the other image models because it's a model relation
# has_many, belongs_to and the model.permalink will be based on the
# uploader's respective model and not the Garage model
# eg. car_uploader.rb = model.permalink = CarImage.permalink
# I would like it to refer to to Garage.permalink at all times.
"garage/#{model.permalink}/#{mounted_as}/"
end
end
# CarrierWave car and motorcycle uploaders
car_uploader.rb
# Fails to upload because it doesn't know what permalink is
end
motorcycle_uploader.rb
# Fails to upload because it doesn't know what permalink is
end
Apologies if I wants so clear but a big thanks for any insight given.
probably the easiest way would be to delegate the permalink to the parent on the model