# Acts\_as\_attachment custom filename - parent name

**URL:** <https://www.ruby-forum.com/t/acts-as-attachment-custom-filename-parent-name/89344>\
**Category:** Rails\
**Created:** [March 19, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-custom-filename-parent-name/89344 "2007-03-19T18:36:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 19, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-custom-filename-parent-name/89344/1 "2007-03-19T18:36:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 19, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-custom-filename-parent-name/89344/2 "2007-03-19T19:02:10Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 20, 2007, 11:00am UTC](https://www.ruby-forum.com/t/acts-as-attachment-custom-filename-parent-name/89344/3 "2007-03-20T11:00:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 20, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-custom-filename-parent-name/89344/4 "2007-03-20T13:02:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 20, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-custom-filename-parent-name/89344/5 "2007-03-20T15:20:53Z")

</div>

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
