关于FactoryGirl的问题

时间:2011-03-19 14:26:22

标签: ruby-on-rails-3 factory-bot

我正在阅读documentation of Factory Girl,并遇到了这个代码块。

#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

我是Factory Girl的新手,但第一个代码示例不应该为new_record返回true吗?我很困惑为什么如果建立和保存帖子,它返回false。

1 个答案:

答案 0 :(得分:2)

new_record?()公众
如果尚未保存此对象,则返回true - 也就是说,该对象的记录尚不存在;否则,返回false 在最新稳定版本的Rails上不推荐使用此方法。

修改
哎呀,看起来链接坏了。固定。 new_record?