-3

When I am using the Rails console I can type either:

Paper.new(name: "deva", body: "testing")
Paper.create(name: "deva", body: "testing")

and it seems like the same result. What exactly is the difference between .new and .create?

MrDanA
  • 11,489
  • 2
  • 36
  • 47
Devux
  • 63
  • 10

2 Answers2

2

aa = Paper.new(name: "deva",body: "testing") In the above case you have to do do .save then only it will generate the id for that record if is valid data, else it will do roll back and it not stored in database.

and here you also can add more field which is in your data table like aa.contact_no = "1234" aa.save

Paper.create(name: "deva",body: "testing") In this case it will try to create the record at the same time and go through the validations.

Sudhir
  • 146
  • 1
  • 4
1

new only initialize the object and do not save the object in the database

create initializes as well as save it if all validations are met.

for help see this: http://apidock.com/rails/ActiveRecord/Base/create/class

Athar
  • 3,258
  • 1
  • 11
  • 16