2011-03-19 63 views
2

我正在閱讀documentation of Factory Girl並遇到此代碼塊。關於FactoryGirl的問題

#The behavior of the association method varies depending on the build strategy used for the parent object. 

#Builds and saves a User 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 

我是新工廠女孩,但應該不是第一個代碼示例new_record返回true?我很困惑,爲什麼它會返回false,如果這個帖子是建立和保存的。

回答

2

new_record()公共
返回true如果此對象尚未保存 - 也就是說,對象還不存在一個記錄?;否則,返回false。
此方法在最新的Rails穩定版本上已棄用。

編輯:
哎呀,看起來像鏈接被打破。固定。 new_record?

+0

啊。我懂了。這是一個相當混亂的術語,所以我很高興它已被棄用。我想他們還沒有到處去更新文檔。 http://api.rubyonrails.org/classes/ActiveRecord/Base.html – picardo 2011-03-19 15:03:33