关于工厂女孩的问题
我正在阅读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 。
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 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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
new_record?() 公开
如果该对象尚未保存,即该对象的记录尚不存在,则返回 true;否则,返回 false。
此方法在 Rails 的最新稳定版本中已弃用。
编辑:
哎呀,看起来链接已损坏。固定的。
new_record?
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?