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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:14:33+00:00 2026-06-07T07:14:33+00:00

I am working through Head First Rails and I came across some code that

  • 0

I am working through Head First Rails and I came across some code that is confusing me a little. The goal of the code is to check and see if any error was made when a new record is being created. If there is an error, then the goal is the redisplay the page. If there is no error, the goal is to save the record to the database. Here is the controller code that the book gives:

def create 
    @ad = Ad.new(params[:ad])
    if @ad.save
        redirect_to "/ads/#{@ad.id}"
    else
        render :template => "ads/new"
    end
end

The only thing about this code that confuses me is the fact the line (if @ad.save). Now, I know this line is testing to see if there are errors. If there are, it returns false, and if there are not, it returns true. However, I noticed that if no errors exist (it returns true), the record is actually saved. I thought “if” statments in ruby just tested a condition, but in this case, the condition is being tested AND executed. The strange this is that if I add another @ad.save, the database DOES NOT save the record twice. Like so:

def create 
    @ad = Ad.new(params[:ad])
    if @ad.save
            @ad.save
        redirect_to "/ads/#{@ad.id}"
    else
        render :template => "ads/new"
    end
end

This code does the exact same thing as the first bit of code. Why in the first bit of code is the @ad.save being executed, and how come on the second bit of code the @ad.save is not executed twice (only one record is created)?

  • 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-07T07:14:36+00:00Added an answer on June 7, 2026 at 7:14 am

    Your assumption about if statements in ruby is incorrect. They can in fact execute code.

    def return_true
      puts 'inside method'
      true
    end
    
    if return_true
      puts "it ran some code"
    end
    
    # output will be:
    # inside method
    # it ran some code
    

    In your second example the save is being run at least once. If the result of @as.save is truthy then it’s run a second time. If it is going through the first part of the if branch then something else is preventing it from being saved to the database twice but there’s not enough info for me to tell why. It could be that you have a unique contraint. Try doing @ad.save!. The bang version will throw an error if there are any validation errors.

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

Sidebar

Related Questions

I am working my way through Head First Rails, and I keep seeing =>
I am currently working through Michael Hartl's Rails Tutorial while experimenting with some other
I'm working my way through 'head first design patterns' and want to use this
I have some terribly written ASP.NET code that is just not working right (go
I am seeing a problem with some Scala 2.7.7 code I'm working on, that
I'm working my way through Head First C#, and I'm a bit confused on
I am working my way through a book called Head First C#. It doesnt
I am working through the EditableGrid demos modifying the code to understand it and
I'm working through some android tutorials right now in preparation for a two week
Hi i'm working through the Hartl's Ruby on Rails Tutorial and i'm stuck with

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.