我一直无法找到Rails中的.build方法的任何文档(我目前使用的是2.0.2)。
通过实验,您可以使用构建方法在保存记录之前将记录添加到has_many关系中。
例如:
class Dog < ActiveRecord::Base has_many :tags belongs_to :person end class Person < ActiveRecord::Base has_many :dogs end # rails c d = Dog.new d.tags.build(:number => "123456") d.save # => true这将正确保存狗和标签与外键。 这在belongs_to关系中似乎不起作用。
d = Dog.new d.person.build # => nil object on nil.build我也试过
d = Dog.new d.person = Person.new d.save # => true在这种情况下, Dog的外键没有设置,因为在保存的时候,新的人没有id,因为它还没有被保存。
我的问题是:
构建工作如何使Rails足够聪明,找出如何以正确的顺序保存记录?
如何在belongs_to关系中做同样的事情?
在哪里可以找到有关此方法的任何文档?
谢谢
I have been unable to find any documentation on the .build method in Rails (i am currently using 2.0.2).
Through experimentation it seems you can use the build method to add a record into a has_many relationship before either record has been saved.
For example:
class Dog < ActiveRecord::Base has_many :tags belongs_to :person end class Person < ActiveRecord::Base has_many :dogs end # rails c d = Dog.new d.tags.build(:number => "123456") d.save # => trueThis will save both the dog and tag with the foreign keys properly. This does not seem to work in a belongs_to relationship.
d = Dog.new d.person.build # => nil object on nil.buildI have also tried
d = Dog.new d.person = Person.new d.save # => trueThe foreign key in Dog is not set in this case due to the fact that at the time it is saved, the new person does not have an id because it has not been saved yet.
My questions are:
How does build work so that Rails is smart enough to figure out how to save the records in the right order?
How can I do the same thing in a belongs_to relationship?
Where can I find any documentation on this method?
Thank you
最满意答案
记录在哪里
从“ Module ActiveRecord :: Associations :: ClassMethods ”中的has_many关联下的API文档
collection.build(attributes = {},...)返回已经通过属性实例化并通过外键链接到此对象但尚未保存的集合类型的一个或多个新对象。 注意:只有在相关对象已经存在的情况下才可以使用,否则为零!
建立相反方向的答案是略有改变的语法。 在你的例子与狗,
Class Dog has_many :tags belongs_to :person end Class Person has_many :dogs end d = Dog.new d.build_person(:attributes => "go", :here => "like normal")甚至
t = Tag.new t.build_dog(:name => "Rover", :breed => "Maltese")您也可以使用create_dog将其立即保存(就像您可以在集合中调用的相应的“创建”方法一样)
轨道如何聪明? 这是魔术(或者更准确地说,我只是不知道,会爱找出来!)
Where it is documented:
From the API documentation under the has_many association in "Module ActiveRecord::Associations::ClassMethods"
collection.build(attributes = {}, …) Returns one or more new objects of the collection type that have been instantiated with attributes and linked to this object through a foreign key, but have not yet been saved. Note: This only works if an associated object already exists, not if it‘s nil!
The answer to building in the opposite direction is a slightly altered syntax. In your example with the dogs,
Class Dog has_many :tags belongs_to :person end Class Person has_many :dogs end d = Dog.new d.build_person(:attributes => "go", :here => "like normal")or even
t = Tag.new t.build_dog(:name => "Rover", :breed => "Maltese")You can also use create_dog to have it saved instantly (much like the corresponding "create" method you can call on the collection)
How is rails smart enough? It's magic (or more accurately, I just don't know, would love to find out!)
Ruby on Rails。(Ruby on Rails. How do I use the Active Record .build method in a :belongs to relationship?)我一直无法找到Rails中的.build方法的任何文档(我目前使用的是2.0.2)。
通过实验,您可以使用构建方法在保存记录之前将记录添加到has_many关系中。
例如:
class Dog < ActiveRecord::Base has_many :tags belongs_to :person end class Person < ActiveRecord::Base has_many :dogs end # rails c d = Dog.new d.tags.build(:number => "123456") d.save # => true这将正确保存狗和标签与外键。 这在belongs_to关系中似乎不起作用。
d = Dog.new d.person.build # => nil object on nil.build我也试过
d = Dog.new d.person = Person.new d.save # => true在这种情况下, Dog的外键没有设置,因为在保存的时候,新的人没有id,因为它还没有被保存。
我的问题是:
构建工作如何使Rails足够聪明,找出如何以正确的顺序保存记录?
如何在belongs_to关系中做同样的事情?
在哪里可以找到有关此方法的任何文档?
谢谢
I have been unable to find any documentation on the .build method in Rails (i am currently using 2.0.2).
Through experimentation it seems you can use the build method to add a record into a has_many relationship before either record has been saved.
For example:
class Dog < ActiveRecord::Base has_many :tags belongs_to :person end class Person < ActiveRecord::Base has_many :dogs end # rails c d = Dog.new d.tags.build(:number => "123456") d.save # => trueThis will save both the dog and tag with the foreign keys properly. This does not seem to work in a belongs_to relationship.
d = Dog.new d.person.build # => nil object on nil.buildI have also tried
d = Dog.new d.person = Person.new d.save # => trueThe foreign key in Dog is not set in this case due to the fact that at the time it is saved, the new person does not have an id because it has not been saved yet.
My questions are:
How does build work so that Rails is smart enough to figure out how to save the records in the right order?
How can I do the same thing in a belongs_to relationship?
Where can I find any documentation on this method?
Thank you
最满意答案
记录在哪里
从“ Module ActiveRecord :: Associations :: ClassMethods ”中的has_many关联下的API文档
collection.build(attributes = {},...)返回已经通过属性实例化并通过外键链接到此对象但尚未保存的集合类型的一个或多个新对象。 注意:只有在相关对象已经存在的情况下才可以使用,否则为零!
建立相反方向的答案是略有改变的语法。 在你的例子与狗,
Class Dog has_many :tags belongs_to :person end Class Person has_many :dogs end d = Dog.new d.build_person(:attributes => "go", :here => "like normal")甚至
t = Tag.new t.build_dog(:name => "Rover", :breed => "Maltese")您也可以使用create_dog将其立即保存(就像您可以在集合中调用的相应的“创建”方法一样)
轨道如何聪明? 这是魔术(或者更准确地说,我只是不知道,会爱找出来!)
Where it is documented:
From the API documentation under the has_many association in "Module ActiveRecord::Associations::ClassMethods"
collection.build(attributes = {}, …) Returns one or more new objects of the collection type that have been instantiated with attributes and linked to this object through a foreign key, but have not yet been saved. Note: This only works if an associated object already exists, not if it‘s nil!
The answer to building in the opposite direction is a slightly altered syntax. In your example with the dogs,
Class Dog has_many :tags belongs_to :person end Class Person has_many :dogs end d = Dog.new d.build_person(:attributes => "go", :here => "like normal")or even
t = Tag.new t.build_dog(:name => "Rover", :breed => "Maltese")You can also use create_dog to have it saved instantly (much like the corresponding "create" method you can call on the collection)
How is rails smart enough? It's magic (or more accurately, I just don't know, would love to find out!)
发布评论