# Users and Photos - Is this the best way?

**URL:** <https://www.ruby-forum.com/t/users-and-photos-is-this-the-best-way/116868>\
**Category:** Rails\
**Created:** [October 10, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/users-and-photos-is-this-the-best-way/116868 "2007-10-10T15:56:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [October 10, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/users-and-photos-is-this-the-best-way/116868/1 "2007-10-10T15:56:08Z")

</div>

Hi,

I have a user and photo model where a user has one photo (using  
attachment\_fu).

I have copied my user actions for create and update below, and I was  
wondering if someone could suggest a better method. I’m just not sure  
this is the best way to do this…

def create  
@user = User.new(params[:user])  
#Create a new photo if the info is sent  
if !params[:photo][:uploaded\_data].blank?  
@user.photo = Photo.create(params[:photo])  
end  
@user.save!  
self.current\_user = @user  
redirect\_back\_or\_default(’/’)  
flash[:notice] = “Thanks for signing up!”  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘new’  
end

and

def update  
@user = User.find(params[:id])  
respond\_to do |format|  
if @user.update\_attributes(params[:user])  
if !params[:photo][:uploaded\_data].blank?  
#Create new photo for the user or find the existing one  
@photo = @user.photo ||= Photo.new  
@photo = @user.photo.build(params[:photo])  
@photo.save  
end  
flash[:notice] = ‘Your account was successfully updated.’  
format.html { redirect\_to users\_url }  
format.xml { head :ok }  
else  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @user.errors.to\_xml }  
end  
end  
end

I would find any help / feedback useful.

Thanks!

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [October 11, 2007, 1:44am UTC](https://www.ruby-forum.com/t/users-and-photos-is-this-the-best-way/116868/2 "2007-10-11T01:44:22Z")

</div>

If anyone could also give some tips on how to delete the old  
attachment (photo) once a new one is created I would appreciate it.

I keep getting an error message when I try to delete the old  
attachment before creating the new one regarding access to frozen  
hashes.

Thanks.

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [October 12, 2007, 6:02am UTC](https://www.ruby-forum.com/t/users-and-photos-is-this-the-best-way/116868/3 "2007-10-12T06:02:10Z")

</div>

Incase anyone is interested / need this, this is the final update  
action I have:

def update  
@user = User.find(params[:id], :include =\> :photo)  
respond\_to do |format|  
if @user.update\_attributes(params[:user])  
# If there is photo data, create the new photo  
if !params[:photo][:uploaded\_data].blank?  
# Look to see if a previous photo exists  
@old\_photo = Photo.find\_by\_user\_id(@user.id) if  
params[:photo][:uploaded\_data]  
if @old\_photo.nil?  
@photo = @user.photo ||= Photo.new  
@photo = @user.photo.build(params[:photo])  
@photo.save  
else  
@photo = @user.photo  
@photo.update\_attributes(params[:photo])  
end  
end  
flash[:notice] = ‘Your account was successfully updated.’  
format.html { redirect\_to users\_url }  
format.xml { head :ok }  
else  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @user.errors.to\_xml }  
end  
end  
end
