I’m trying to customize the filename in aaa and I know that you can
over-ride the full_filename method in the model, but I can’t figure out
how to do exactly what I want.
Imagine you have three models:
class ActionMovies < ActiveRecord::Base
has_many :actors
end
class Actors < ActiveRecord::Base
has_one :mug_shot
belongs_to :action_movies
end
class MugShot < ActiveRecord::Base
acts_as_attachment :storage => :file_system, :file_system_path =>
‘public’
belongs_to :actors
end
I’d like the path to look like this:
public/action_movie_name/actors/actor_name/mug_shot/actor_name.jpg
The user uploads a picture named: my_picture_of_actor_in_las_vegas.jpg
So, I have to retrieve the move name and actor name and also rename the
file.
So, I tried to over-ride the full_filename method something like this:
def full_filename
file_system_path = (thumbnail ? thumbnail_class :
self).attachment_options[:file_system_path]
actor_name = sanitize_filename(Actor.find(self.actor_id))
action_movie_name =
sanitize_filename(ActionMovie.find(actor.action_movie_id))
File.join(RAILS_ROOT, file_system_path, action_movie_name, ‘actors’,
actor_name, ‘mug_shot’, thumbnail_name_for(thumbnail))
end
Obviously this does not address changing the original filename from
my_picture_of_actor_in_las_vegas.jpg to actor_name.jpg. Also, it
doesn’t work.
The actor and therefore the action_movie cannot be retrieved. I’m sure
that this is because I’m calling attributes that haven’t been saved yet
(like actor_id). Unfortunately, I’m too green on ruby and rails to know
the best way to go about this…
Any help would be greatly appreciated. I plan to add this to a tutorial
I’m writing for aaa based on a previous question answered here.
Thanks a lot.
Shagy