Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - in Rails Console, when you save and get false, How can you find out why?

In Rails Console, I'm creating a record and then entering @record.save and I get false but I can't figure out why? Is there a way in Rails C to output why the save failed?

Thanks

like image 943
AnApprentice Avatar asked Mar 22 '11 00:03

AnApprentice


People also ask

How do you save in rails console?

The #save method triggers a SQL INSERT that creates a new record on the albums table for our album object as seen between the BEGIN and COMMIT . The database returns the value true to indicate that the save was successful.

What does .save do in Ruby?

The purpose of this distinction is that with save! , you are able to catch errors in your controller using the standard ruby facilities for doing so, while save enables you to do the same using standard if-clauses.


1 Answers

The errors are accessed through the errors instance method. Example:

ruby-1.8.7-p334 :001 > c = Company.new
=> #<Company id: nil, name: nil, link: nil, created_at: nil, updated_at: nil> 
ruby-1.8.7-p334 :002 > c.save
=> false 
ruby-1.8.7-p334 :003 > c.errors
=> #<OrderedHash {:name=>["can't be blank"]}> 
like image 92
Alberto Santini Avatar answered Sep 20 '22 09:09

Alberto Santini