swaminadhan
Joined: 14 Sep 2009 Posts: 73
|
Posted: Mon Dec 28, 2009 12:08 pm Post subject: Differences between create and save methods in Active record |
|
|
hello developers !
Generally in most of the applications we will use create and save methods to create and store the data in the database... So let us discuss what these methods will do.
Both these create and save methods belongs to the Active record base class
But there is difference in these methods that is the create is a class method where as save is an instance method.
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
The parameters can be either be a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.
for example :
User.create(:first_name => 'john')
From the above example we can clearly say that,the create method is a class method.
When we comes to the save method,If the model is new a record gets created in the database, otherwise the existing record gets updated.
If the performed validation is true validations then the save method will return true and save the content in the database If any of them fail
the action is canceled and save returns false.
similarly, There's a series of callbacks associated with save. If any of the
before_* callbacks return false the action is canceled and save returns false.
Here we will not call the method followed by the class. To perform the operation of save the data should be exists before. To update the data we will use the save operation.
for example :
@user.save
From the above example we can say that the save is an instance method.
thank you
swaminadhan. |
|