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)?
Your assumption about
ifstatements in ruby is incorrect. They can in fact execute code.In your second example the save is being run at least once. If the result of
@as.saveis truthy then it’s run a second time. If it is going through the first part of theifbranch 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.