Rails: Difference between create and new methods in ActiveRecord?

I'm following a Rails 3.0 tutorial by lynda.com.

What's the difference between these two lines?

first_page = Page.new(:name => "First page")

first_page = Page.create(:name => "First page")

By the way, this is great tutorial; I recommend it for any other newbies like me.

Answers


Basically the new method creates an object instance and the create method additionally tries to save it to the database if it is possible.

Check the ActiveRecod::Base documentation:

create method Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.

new method New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names).


Need Your Help

add static pdf to current generated on the fly pdf, using itext

pdf merge itext document writer

I have a problem adding already existed pdf into current pdf I just generated.

multithreading error with ABAddressBookRef

objective-c multithreading contacts addressbook

I have problems with the threads at the recorer for, I have a contact list of 600 and 6 shows me the following error.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.