Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between first! and first method in Rails

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.

like image 281
thank_you Avatar asked Oct 24 '12 22:10

thank_you


People also ask

What is ActiveRecord in Ruby on Rails?

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.

What is ORM in ROR?

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.

What is ActiveRecord base?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What's the purpose of ActiveRecord?

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.


1 Answers

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

like image 108
Prakash Murthy Avatar answered Sep 28 '22 11:09

Prakash Murthy