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.
What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.
1.2 Object Relational Mapping Object Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
Active Record uses the most obvious approach, putting data access logic in the domain object. This way all people know how to read and write their data to and from the database.
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
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