# Using foreign key in store\_dir

**URL:** <https://www.ruby-forum.com/t/using-foreign-key-in-store-dir/114083>\
**Category:** Rails\
**Created:** [September 7, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/using-foreign-key-in-store-dir/114083 "2007-09-07T18:58:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 7, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/using-foreign-key-in-store-dir/114083/1 "2007-09-07T18:58:21Z")

</div>

I am new to Ruby/Rails, and I’m writing an app that allows the user  
to  
upload mulitple images to attach to a user. The user data is stored  
in  
the Entity model, and the picture data (including file\_column) is  
stored  
in the Picture model, which has a foreign key to the entity.id. I  
want  
to store the pictures in a directory that has the Entity’s id as the  
last directory name, not the picture’s id. Base upon what I’ve read,  
I  
have to use a callback procedure in Picture to create the appropriate  
directory. This is what I came up with:

_entity.rb_  
class Entity \< ActiveRecord::Base  
validates\_presence\_of :lastname, :address1, :zip, :phone

```
belongs_to :entitytype
has_and_belongs_to_many :entitysubtypes
belongs_to :state
belongs_to :city
belongs_to :agency
belongs_to :agent
belongs_to :screentemplate
has_many :comments
has_many :pictures

```

end

_picture.rb_  
class Picture \< ActiveRecord::Base  
belongs\_to :entity  
file\_column :image,  
:web\_root =\> “/picture/entity/”,  
:store\_dir =\> :new\_store\_dir

```
def new_stor_dir
    return "/picture/entity/" + :entity.id + "/"
end

```

_entity\_controller.rb_  
def upload  
@entity = Entity.find(params[“id”])  
@pictures = @entity.pictures.build(params[:pictures])  
@entity.pictures.each { |pic| pic.attributes =  
params[:picture][pic.id.to\_s] }  
if @entity.update\_attributes(params[:entity])  
flash[:notice] = “Pictures uploaded.”  
else  
flash[:notice] = “Problem uploading pictures.”  
end  
render :action =\> ‘show’, :id =\> @entity  
end

I must be doing something wrong because I get no file name in the  
database. I suspect that, since it worked fine without the callback  
label in store\_dir, it must be something I’m doing. Any ideas? Any  
help  
would be greatly appreciated!

Thanks.
