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

  • Home
  • SEARCH
  • 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 8670597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:44:42+00:00 2026-06-12T18:44:42+00:00

In a Rails controller code def create @post = Post.new(params[:post]) @post.random_hash = generate_random_hash(params[:post][:title]) if

  • 0

In a Rails controller code

def create
  @post = Post.new(params[:post])
  @post.random_hash = generate_random_hash(params[:post][:title])
  if @post.save
    format.html { redirect_to @post }
  else
    format.html { render action: "new" }
  end
end

Should the first two lines of the definition be put inside if @post.save or not? If the post is not saved, will the Post object created by Post.new still be put in the database?

  • 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-06-12T18:44:43+00:00Added an answer on June 12, 2026 at 6:44 pm
    1. Should the first two lines of the definition be put inside if @post.save or not?

      Most certainly not. If you change it to the following as you suggest:

      def create
        if @post.save
          @post = Post.new(params[:post])
          @post.random_hash = generate_random_hash(params[:post][:title])
          format.html { redirect_to @post }
        else
          format.html { render action: "new" }
        end
      end
      

      Then it won’t work at all. There is no @post to call save on.

    2. If the post is not saved, will the Post object created by Post.new still be put in the database?

      Of course not. That’s what saving does: saves the object in the database. If you don’t call save on the Post object, or the save returns false (which would happen because a validation failed), the object was not stored in the database. Post.new simply creates a new Post object in-memory—it doesn’t touch the database at all.

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

Sidebar

Related Questions

I have a Rails controller with a create method, and I want to POST
Both my Rails model and controller code need to write files to the file
In my Rails controller I have something like: def authenticate_user! if not current_user #
i would like to create a Rails controller that download a serie of jpg
When running functional tests on my controller code in a Rails 3 project, I
In my Rails app, I'm trying to use jQuery ajax to create a new
I'm trying to create devise users via POST using JSON. The current code of
I'm new to rails and getting the following error: NameError in FriendshipsController#create uninitialized constant
I am trying to implement the following code in index.html.erb on rails and the
I am new to Rails and am trying to create a child record in

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.