BCHodo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Sep-07 16:55 UTC
Using foreign key in store_dir
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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---