rspec - difference between calling create and new() in ruby with rails -


suppose have class try. trying create object use in 1 of examples in rspec file.

i tried writing let(:obj){obj = try.new()} , accessing in example gave error. when wrote llet(:obj){obj = try.create}, use obj , call functions without error.

what difference when write try.create , try.new() in rspec file?

from activerecord::base documentation:

create(attributes = nil) {|object| ...} 

creates object (or multiple objects) , saves database, if validations pass. resulting object returned whether object saved database or not.

new(attributes = nil) {|self if block_given?| ...} 

new objects can instantiated either empty (pass no construction parameter) or pre-set attributes not yet saved (pass hash key names matching associated table column names). in both instances, valid attribute keys determined column names of associated table — hence can‘t have attributes aren‘t part of table columns. create instantiates new object, validates it, , saves database. , new creates local object not attempt validate or save db.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -