From the ActiveRecord::Base documentation:
create(attributes = nil) {|object| ...}
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(attributes = nil) {|self if block_given?| ...}
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). In both instances, valid attribute keys are determined by the column names of the associated table — hence you can‘t have attributes that aren‘t part of the table columns.
So create
instantiates the new object, validates it, and then saves it to the database. And new
only creates the local object but does not attempt to validate or save it to the DB.
create = "new object" + ”validates“ + ”save to db“
new = “new object”
在create的内部方法中,先调用new ,再调用save:new + save = create
def create(attributes = nil, options = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, options, &block) }
else
object = new(attributes, options, &block)
object.save
object
end
end
转载自:
http://stackoverflow.com/questions/2472393/rails-new-vs-create
http://stackoverflow.com/questions/9791386/differences-between-new-save-and-create