AnApprentice
AnApprentice

Reputation: 111040

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

Upvotes: 14

Views: 7162

Answers (2)

Alberto Santini
Alberto Santini

Reputation: 6562

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"]}> 

Upvotes: 25

Shiv
Shiv

Reputation: 8412

If it is false then there are errors

In the console type

@record.errors

Upvotes: 9

Related Questions