Ruby on Rails。如何在 :belongs to 关系中使用 Active Record .build 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/783584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:11:05  来源:igfitidea点击:

Ruby on Rails. How do I use the Active Record .build method in a :belongs to relationship?

ruby-on-railsactiverecordforeign-keys

提问by stellard

I have been unable to find any documentation on the .build method in Rails (i am currently using 2.0.2).

我一直无法在 Rails 中找到关于 .build 方法的任何文档(我目前使用的是 2.0.2)。

Through experimentation it seems you can use the build method to add a record into a has_manyrelationship before either record has been saved.

通过实验,您似乎可以使用 build 方法has_many在保存记录之前将记录添加到关系中。

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 # => true

This will save both the dog and tag with the foreign keys properly. This does not seem to work in a belongs_torelationship.

这将正确保存带有外键的狗和标签。这在belongs_to关系中似乎不起作用。

d = Dog.new
d.person.build # => nil object on nil.build

I have also tried

我也试过

d = Dog.new
d.person = Person.new
d.save # => true

The foreign key in Dogis 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.

Dog在这种情况下,外键 in没有设置,因为在保存时,新人没有 id,因为它还没有保存。

My questions are:

我的问题是:

  1. How does build work so that Rails is smart enough to figure out how to save the records in the right order?

  2. How can I do the same thing in a belongs_torelationship?

  3. Where can I find any documentation on this method?

  1. 构建是如何工作的,以便 Rails 足够聪明以找出如何以正确的顺序保存记录?

  2. 我怎么能在一段belongs_to关系中做同样的事情?

  3. 我在哪里可以找到有关此方法的任何文档?

Thank you

谢谢

回答by BushyMark

Where it is documented:

在哪里记录:

From the API documentation under the has_many association in "Module ActiveRecord::Associations::ClassMethods"

来自“模块 ActiveRecord::Associations::ClassMethods”中has_many 关联下的 API 文档

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!

collection.build(attributes = {}, ...) 返回一个或多个集合类型的新对象,这些对象已经用属性实例化并通过外键链接到该对象,但尚未保存。注意:这仅在关联对象已经存在时才有效,如果它为零则无效!

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)

您还可以使用 create_dog 立即保存它(很像您可以在集合上调用的相应“create”方法)

How is rails smart enough? It's magic (or more accurately, I just don't know, would love to find out!)

Rails 的智能程度如何?这很神奇(或者更准确地说,我只是不知道,很想知道!)

回答by nehpets

@article = user.articles.build(:title => "MainTitle")
@article.save