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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:39:54+00:00 2026-05-19T15:39:54+00:00

Is there a way to do a save to an existing record instead of

  • 0

Is there a way to do a “save” to an existing record instead of using update_attributes?

Half of the values I’m saving are calculated from user inputs and so are not present in the params hash. Also, there can be multiple user review cycles, which means that :id drops out of the params hash (I’m saving it in the sessions hash).

To me it seems conceptually simpler to “save” on the final version of @post. I ended up saving the final version of @post in an attributes hash, then going back to the database to get the original record, then doing update_attributes. Hopefully that 2nd retrieval of the record is cached? Actually now it’s easy to generate a list of changes in a given edit, so maybe I’ll stay with this if it works, but it seems awkward.

What is the “Rails Way?” I’m new around here and want to fit in.

<form is submitted>
@post = Post.new(params[:post])
<lots of calculations and validity checking>
finalattrhash = @post.attributes
@post = nil
@post = post.find(session[:postid])

respond_to do |format|
  if @post.update_attributes(finalattrhash)
    session[:postid] = nil
    format.html { redirect_to(@post, :notice => 'post was successfully updated.') }
  else ... 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-19T15:39:54+00:00Added an answer on May 19, 2026 at 3:39 pm

    Your complex handling and validations should be done inside the model. A fat controller is almost always worse than a fat model.

    To make it clear, let’s make an example.

    Suppose you have a model Post. You have title and content stored in the database. And suppose you don’t want the use to directly input those fields, but other four fields: trip_name, trip_date, visited and with_who. (Of course it is not very ‘real case’ :D)

    class Post < ActiveRecord::Base
      # DB fields: title:string, content:text
      attr_accessor :trip_name, :trip_date, :visited, :with_who
      attr_protected :title, :content # This line could be ignored, but to only protected these two fields from mass assignment.
    
      before_save :complex_handles
    
      validates :title, :presence => true, :uniqueness => true
      validates :content, :presence => true
    
      private
    
      def complex_handles
        @title = @trip_name + " " + @trip_date
        @content = @visited + " " + @with_who
      end
    
    end
    

    So your form would give you the following hash:

    params[:post] = {
      :trip_name => "some trip",
      :trip_date => "2010-01-29",
      :visited => "Hong Kong",
      :with_who => "with my GF"
    }
    

    This should be a normal simple form and the controller would be the simplest form too:

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to(@post, :notice => 'post was successfully updated.') }
      else
        ...
      end
    end
    

    As all those complex logics and validations are related to the model itself, it is best to keep inside the model. In this way, your controller and view would be very clean.

    ===== UPDATED =====

    If my understanding is correct, I would guess you have a form with two buttons (one is review and one is finalize). And for review, you just update the fields, without saving to the database.

    So this would be easier to give the two submit buttons a different name attribute. And in your controller:

    respond_to do |format|
      if (params[:action] == "finalize" && @post.update_attributes(params[:post]))
        || (params[:action] == "review" && @post.attributes = params[:post] && @post.valid?)
        format.html { redirect_to(@post, :notice => 'post was successfully updated.') }
      else
        ...
      end
    end
    

    This part seems a little bit tricky. As the params[:action] could be one value in one time, so you won’t mess up the checking.

    So if it is finalize, you just call the update_attributes to save it. Else, you assign those attributes without saving, and then check if it is valid.

    However, you have to update the Post model’s code a little bit:

    # Before, I have used:
    before_save :complex_handles
    
    # Now, I change to:
    before_validation :complex_handles
    

    So that the handling would be done before you call @post.valid? in review part. (This should not affect the finalize part)

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

Sidebar

Related Questions

is there a way to save, not save as, from an online flex app
Is there any way to save the state of vim settings with a document?
I'm wondering if there's a way to save window configurations across emacs sessions. I
Is there a one button way to save a screenshot directly to a file
Duplicate of Disable browser save password functionality Is there a way to tell the
Does anyone know if there's a way to get Eclipse to do a Save
Is there way in next piece of code to only get the first record?
Is there any way to check whether a file is locked without using a
Is there a way to save a search information in Visual Studio. For example
Question: Is there a way to use existing objective-c methods to do a full

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.