Consider this code of my Controller:
def create
change_some_db_values
buyer = Buyer.new(params[:buyer])
if buyer.save
redirect_to(:action => 'index')
else
render('new')
end
end
I would like to know in advance if buyer.save
will fail or not. If it's gonna fail I don't want to execute change_some_db_values
. How could I achieve this ?
You can add a check for validity before saving:
def create
buyer = Buyer.new(params[:buyer])
if buyer.valid?
change_some_db_values
end
if buyer.save
redirect_to(:action => 'index')
else
render('new')
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With