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
Post a Comment