Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:18:46+00:00 2026-05-31T02:18:46+00:00

I’m very new to Rails, so it’s likely there’s a simple concept here that

  • 0

I’m very new to Rails, so it’s likely there’s a simple concept here that I’m just not getting, but here’s what’s happening.

I have a User model (powered by Devise), and each user has it’s own respective photo attribute. I know I could have included the photo as part of the User, but the photos are actually the core content of the site, so I preferred them to be their own table. The photo model has a paperclip attachment which handles the actual photo file.

Here’s the issue: Everything works according to plan when I upload a photo as a user, but for some reason, when I return to the photo upload page, the photo I just uploaded gets deleted. I’ve tracked it down to this line of code:

@photo = @user.build_photo

If I don’t call that, the form for the upload throws a nil class error because @user.photo doesn’t exist, but when I DO call it, it deletes the previously uploaded photo, which is weird, because as far as I know, it’s the create function that alters the database, not the build.

Here’s what the server shows:

Started GET “/settings” for 127.0.0.1 at 2012-03-08 10:19:21 -0800
Processing by SettingsController#index as HTML User Load (0.3ms)
SELECT users.* FROM users WHERE users.id = 6 LIMIT 1 Photo
Load (0.3ms) SELECT photos.* FROM photos WHERE photos.user_id
= 6 LIMIT 1 (0.2ms) BEGIN [paperclip] Scheduling attachments for deletion. SQL (0.6ms) DELETE FROM photos WHERE photos.id = 20
[paperclip] Deleting attachments.

And here’s a couple of my models and controllers:

class SettingsController < ApplicationController
    def index
        @user = current_user
        @photo = @user.build_photo
    end
end

<h1>Settings Page</h2>
<%= image_tag @user.photo.the_photo.url(:medium) %>
<%= form_for [@user, @photo], :html => { :multipart => true } do |f| %>
    <%= f.file_field :the_photo %>
    <%= f.submit %>
<% end %>


class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :photo_attribute

  has_one :photo, :dependent => :destroy
  accepts_nested_attributes_for :photo
end

class PhotosController < ApplicationController
    def create
        @user = current_user
        @photo = @user.create_photo(params[:photo])
        redirect_to root_path
    end

    def update
        @user = current_user
        @photo = @user.photo
        if @photo.update_attributes(params[:photo])
            redirect_to settings_path
        else
            redirect_to settings_path
        end
    end

    def destroy
    end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:18:47+00:00Added an answer on May 31, 2026 at 2:18 am

    As you’ve discovered, calling @user.build_photo will remove a photo for that user if one already exists. You just need to skip doing the build in that case:

    @photo = @user.photo || @user.build_photo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.