Acts_as_attachment custom filename - parent name


#1

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. :expressionless:

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


#2

Oops,

Well, there are a few typos in the example. Like this line:

actor_name = sanitize_filename(Actor.find(self.actor_id))

should be more like:

actor_name = sanitize_filename(Actor.find(self.actor_id).name)

though this won’t work either.

Anyway, you get the drift, I hope…


#3

Hi

I am trying to follow.
in the actor_name variable - do you want the actor’s name? if so, isn’t
Actor.find(self.actor_id).name enough? what does the sanitize_filename
do?

Does your user choose the actor for which they are uploading the file
from a
dropdown or type on the actor name, or how does your app know what actor
your user is trying to upload a file for?

Just to make sure that I understand correctly - your Actor model has an
action_movie_id?

assuming that the model instance of the file_upload has actor_id, does
not
have to be saved yet, then maybe these changes will help?
(its possible that I am completely missing the point - had very little
sleep
last night :] )

def full_filename
actor = Actor.find(self.actor_id)
file_system_path = (thumbnail ? thumbnail_class
:self).attachment_options[:file_system_path]
actor_name = actor.name
action_movie_name = ActionMovie.find(actor.action_movie_id).name
File.join(RAILS_ROOT, file_system_path, action_movie_name,
‘actors’,actor_name, ‘mug_shot’, thumbnail_name_for(thumbnail))
end

also, with the changing the filename from
my_picture_of_actor_in_las_vegas.jpg to actor_name.jpg
to
actor_name.jpg - what is your strategy to do this?

regards
ivor


#4

Ivor P. wrote:

Hi

I am trying to follow.
in the actor_name variable - do you want the actor’s name?

Yes

if so, isn’t
Actor.find(self.actor_id).name enough?

No, it doesn’t work. I think it doesn’t work because the record isn’t
saved yet, and therefore you cannot access the attributes in this way,
but I’m really not completely sure.

what does the sanitize_filename
do?

sanitize_filename just removes illegal characters and spaces and turns
them into single underscores.

Does your user choose the actor for which they are uploading the file
from a dropdown or type on the actor name, or how does your app know what actor your user is trying to upload a file for?

The user has selected the action movie and is now filling out a form
with the actor’s name and mug shot file upload.

Just to make sure that I understand correctly - your Actor model has an
action_movie_id?

Yes

assuming that the model instance of the file_upload has actor_id, does
not
have to be saved yet, then maybe these changes will help?
(its possible that I am completely missing the point - had very little
sleep
last night :] )

def full_filename
actor = Actor.find(self.actor_id)
file_system_path = (thumbnail ? thumbnail_class
:self).attachment_options[:file_system_path]
actor_name = actor.name
action_movie_name = ActionMovie.find(actor.action_movie_id).name
File.join(RAILS_ROOT, file_system_path, action_movie_name,
‘actors’,actor_name, ‘mug_shot’, thumbnail_name_for(thumbnail))
end

This is basically the same approach I was taking and no, it doesn’t
work.

also, with the changing the filename from
my_picture_of_actor_in_las_vegas.jpg to actor_name.jpg
to
actor_name.jpg - what is your strategy to do this?

I have no idea. I’ve been picking around the code, trying to find the
best way to do it and haven’t figured it out.

regards
ivor

Thanks for your help.

Shagy


#5

doesn’t look like I can help much, except that the saving issue.

You can create models, give them attributes and use them without ever
saving
them to the database. It is possible however, that the values are never
assigned in the first place, are you using text_field or text_field_tag
(or
ekwivalent for other html components) in your view? text_field applies
the
value entered directly to the model while text_field_tag merely gives
you
the value in the controller.

text_field :actor, :name will automatically assign the name value, while
text_field_tag :name will only provide you with params[:name] in your
controller. in your controller you will then have to do
actor = new Actor(:name => params[:name]) manually.

Also, if you are using text_field then you have to pass a actor model
from
the controller to the view.

I am not the best person to give you advice - have been programming
actionscript for a while and the bit of rails I know is rusty but I seem
to
remember that - perhaps someone else can give you better advice and post
it
here so I can learn aswell.

regards