Difference between first! and first method in Rails

ruby, ruby-on-rails, syntax

Solution

I didn't know there was a `first!` finder method in ActiveRecord. Thanks to your question, now I know :-)

`first!` is the same as `first` except that it raises ActiveRecord::RecordNotFound if no record is found.

More details here : http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-first-21

Problem

What is the difference between `User.first` and `User.first!` in Ruby on Rails? I see the example in the Ruby Guide but there is no explanation between why it's different. As far as I know, `!` is used to represent that the method is changing the variable. *`User` represents a table.

Original source