I was reading through the documentation of Factory Girl and came across this code block.
#The behavior of the association method varies depending on the build strategy used for the parent object.
#Builds and saves a U开发者_开发百科ser and a Post
post = FactoryGirl.create(:post)
post.new_record? # => false
post.author.new_record # => false
# Builds and saves a User, and then builds but does not save a Post
post = FactoryGirl.build(:post)
post.new_record? # => true
post.author.new_record # => false
I'm new to Factory Girl, but shouldn't the first code example return true for new_record? I'm confused why it returns false if the post is built and saved.
new_record?() public
Returns true if this object hasn’t been saved yet — that is, a record for the object doesn’t exist yet; otherwise, returns false.
This method is deprecated on the latest stable version of Rails.
Edit:
Oops, looks like the link was broken. Fixed.
new_record?
精彩评论