2011-03-19 27 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