I implemented a feature that lets users set default profile photos and photo-album cover photos.
I have the following tables in my database:
The action for setting a default profile photo is:
def set_default_profile_photo
photo = Profile.find_by_user_id(current_user.id)
photo.photo_id = params[:photo_id]
photo.save
redirect_to :back
flash[:success] = "Default photo set!"
end
That works fine.
Setting my default album photo was a little trickier:
def set_default_album_photo
photo = PhotoAlbum.where(:id => Photo.where(:id => params[:photo_id]).first.photo_album_id)
photo.first.photo_id = params[:photo_id]
photo.first.save
redirect_to :back
flash[:success] = "Default album photo set!"
end
This is the action that didn't work:
def set_default_album_photo
photo = PhotoAlbum.where(:id => Photo.where(:id => params[:photo_id]).first.photo_album_id)
photo.photo_id = params[:photo_id]
photo.save
redirect_to :back
flash[:success] = "Default album photo set!"
end
I got it working, and the undefined photo_id method error went away, but that was after changing these lines from:
photo.photo_id = params[:photo_id]
photo.save
to:
photo.first.photo_id = params[:photo_id]
photo.first.save
I am not sure what is happening, and why that change made things work, and want to understand what is going on.
I don't have commenting ability, so I was not able to comment on the above post, but I should point out:
Foo.where(:id => X).first is NOT the same as Foo.find(X). Foo.find(X) will raise an exception if there's no record with id = X, while the other one will just return nil.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With