关于工厂女孩的问题

发布于 2024-10-22 23:35:59 字数 683 浏览 1 评论 0原文

我正在阅读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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2024-10-29 23:35:59

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文