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 580625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:29:33+00:00 2026-05-13T14:29:33+00:00

I am developing a complex form that updates several records of one model at

  • 0

I am developing a complex form that updates several records of one model at once, whilst simultaneously updating an associated model. It looks a bit like this:

class Sport
  has_one :photo
end

class Photo
  belongs_to :sport
  acts_as_fleximage
end

class Page
  # the page is not related to either of the previous models
end

Just for a bit of background info, the Page model is a generic model for which the users will be able to create as many as they like (a CMS). In addition, they are given a small number of compulsory “system” pages when they sign up. When they try to edit a system page, the form is slightly different from the generic page form.

One of the system pages is a “Sports” page. Where they can add some text for each of their sports (saved in the “sport” model) and upload a photo (saved in the “photo” model).

I’ve crafted a form which seems to be doing the trick. I won’t post the view, but here is an example of the parameters it sends:

:id => 1
:page => {"title"=>"Our sports"}
:sport => {
  "1" => {
    "description" => "<p>I love playing hockey...</p>"
    "photo_attributes" => {
      "image_file" => #<File:/tmp/RackMultipart20100126-955-k0gxu8-0>,
      "description" => "Me in my hockey kit"
    }
  },
  "2" => { #more of the same}
}

Now, to save all this, my controller/action looks something like this:

def update_sports_page
  @page = Page.find params[:id]
  @page.update_attributes params[:page]
  Sport.update(params[:sport].keys, params[:sport].values)
  redirect_to #etc
end

Now when I edit the sports page, everything saves and updates correctly, EXCEPT, if I change the photo then rather than it updating the existing record in the database, it just creates a new record and sets the sport_id of the old record to NULL.

So eventually, after many edits, there is a huge number of orphan records in the database.

Can anyone spot what I am doing wrong here?

(ps, in case it’s relevant, I’m using fleximage on the Photo model)

  • 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-13T14:29:33+00:00Added an answer on May 13, 2026 at 2:29 pm

    That’s probably the correct behavior, since that association is set :dependent => :nullify by default, not :dependent => :destroy.

    It might be possible to fix it with:

    class Photo
      belongs_to :sport,
        :dependent => :destroy
    end
    

    That should remove orphan records automatically for you.

    You should also be careful to catch exceptions when performing any find or update operation.

    def update_sports_page
      @page = Page.find params[:id]
      @page.update_attributes params[:page]
    
      params[:sport].each do |sport_id, sport_params|
        sport = Sport.find(sport_id)
        sport.update_attributes!(sport_params)
      end
    
      redirect_to #etc
    rescue ActiveRecord::RecordNotFound
      render(:partial => 'page_not_found', :status => :not_found)
    rescue ActiveRecord::RecordInvalid
      render(:action => 'edit')
    end
    

    That’s an example. The edit and update method should have a common “sport loader” mechanism which can handle retrieving all the relevant records for the page instead of having to duplicate this functionality during update. It’s responsible to catch errors on update and display them on the edit page for review.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an algorithmic trading application using complex event processing and I'm looking into
I'm developing with a really complex cms system, and sometimes I need to know
I have a following requirement for a very complex UI. (Complex here means there
I am developing application having business objects created from EF 4.0. The application is
I'm developing a web application based on Struts2 Framework for implementing the MVC, and
We are developing an inventory tracking system. Basically we've got an order table in
I'm developing a WinForms application in C#. I have limited experience in GUI programming,
I have two applications: X and Y. X is the main application and it
Anyone released any Silverlight 4.0 out-of-browser app yet? does it compare to AIR 2.0?
Every time I look for mobile phone development I see Android, WinMobile, iPhone, RIM,

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.