Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
就使用而言,我已经使用过parent.childs.build并且parent.build_child可以互换。可以互换使用这两种方法吗?
parent.childs.build
parent.build_child
如果这两种方法有区别,每种方法分别用在什么情况下?
通常,build_child用于一种has_one情况,并children.build用于has_many.
build_child
has_one
children.build
has_many
不过这里并没有真正的区别——他们每个人都会构建一个新对象(但不保存它),并将新对象的 parent_id 设置为父对象
据我所知,它们指的是相同的方法。为了方便起见,Rails 只是将它放在两个位置。