当在的has_many关系活动记录的对象保存? [英] When are Active Record objects in has_many relationships saved?

查看:147
本文介绍了当在的has_many关系活动记录的对象保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Rails 1.2.3(是的,我知道),并感到困惑的是如何的has_many 对于工程对象持久性。

I'm using Rails 1.2.3 (yeah, I know) and am confused about how has_many works with respect to object persistence.

为了便于举例,我将用这个作为我的声明:

For the sake of example, I'll use this as my declaration:

class User < ActiveRecord::Base
    has_many :assignments
end

class Assignment < ActiveRecord::Base
    belongs_to :user
end

据我所知,这会产生,除其他外,一个方法用户#assignments.build ,它创建一个分配对象,其 USER_ID 是接收实例的 ID (而另一字段作为参数中指定),但不保存该对象在数据库中。对象可以在以后通过调用作业#保存救了!

As I understand it, this generates, among others, a method User#assignments.build, which creates an Assignment object whose user_id is the receiving instance's id (and whose other fields are as specified in the argument), but does not save this object in the database. The object can be saved later by calling Assignment#save!.

不过,和Pragmatic Programmers'的敏捷Web开发使用Rails,第二版的,我一直在使用作为指导和参考哪些,说:

However, The Pragmatic Programmers' Agile Web Development with Rails, Second Edition, which I've been using as a tutorial and reference, says:

如果父对象存在于数据库中,然后添加子   对象的集合将自动保存孩子。

If the parent object exists in the database, then adding a child object to a collection automatically saves that child.

似乎有在这里是一对矛盾。我想知道的是:

There seems to be a contradiction here. What I'd like to know is:

  • 如果我这样做 some_user.assignments.build ,是分配对象保存?
  • 如果我这样做 some_user.assignments&LT;&LT; Assignment.new ,是分配对象保存?
  • 如果我这样做 some_user.assignments&LT;&LT; Assignment.create ,是两个数据库调用制作,或只是一个?那么如果我修改分配对象创建它并将它添加到 some_user.assignments 之间?
  • 在发生什么,如果我保存!分配对象,其相应的用户尚未保存在数据库中?
  • If I do some_user.assignments.build, is the Assignment object saved?
  • If I do some_user.assignments << Assignment.new, is the Assignment object saved?
  • If I do some_user.assignments << Assignment.create, are two database calls made, or just one? What about if I modify the Assignment object between creating it and adding it to some_user.assignments?
  • What happens if I save! an Assignment object whose corresponding User has not yet been saved in the database?

P.S。我不只是使用其原因用户#assignments.create 的一切是因为它不会让我种田了初始化到外部的方法,我想能够做到。我也不想做出多次往返到数据库。

P.S. The reason I don't just use User#assignments.create for everything is because it doesn't let me farm out initialization to an external method, which I'd like to be able to do. I also don't want to make multiple trips to the database.

推荐答案

注:下面的所有控制台测试在Rails的3来看,你可能会在Rails的1不同的输出,你必须运行测试自己比较。

如果您想了解所发生的事情的背后有活动记录的场景有你的Rails控制台方便是非常有价值的。下面是与非保存的对象会发生什么:

Having your rails console handy is extremely valuable if you want to understand what's happening behind the scenes with Active Record. Here's what happens with a non saved object:

u = User.new
#<User id: nil, name: nil, created_at: nil, updated_at: nil> 

u.assignments.build(:name => "example")
#<Assignment id: nil, name: "example", user_id: nil, created_at: nil, updated_at: nil>

u.save
#SQL (0.2ms)  INSERT INTO `users` (`created_at`, `name`, `updated_at`) VALUES ('2012-06-01 19:25:45', NULL, '2012-06-01 19:25:45')
#SQL (0.2ms)  INSERT INTO `assignments` (`created_at`, `name`, `updated_at`, `user_id`) VALUES ('2012-06-01 19:25:45', 'example', '2012-06-01 19:25:45', 1)

正如你所看到的,它们都被保存在新用户保存在同一时间。现在让我们尝试情景二:

As you can see, both are saved at the same time when the new user was saved. Now let's try scenario two:

u = User.create!(:name => "test")
#SQL (0.2ms)  INSERT INTO `users` (`created_at`, `name`, `updated_at`) VALUES ('2012-06-01 19:27:21', 'test', '2012-06-01 19:27:21')
#<User id: 2, name: "test", created_at: "2012-06-01 19:27:21", updated_at: "2012-06-01 19:27:21"> 

u.assignments.build(:name => "example")
#<Assignment id: nil, name: "example", user_id: 2, created_at: nil, updated_at: nil>

所以,从这个我们可以得出结论:

So, from this we can conclude:

如果我做的some_user.assignments.build,被保存在分配对象?

都能跟得上

如果我做some_user.assignments&LT;&LT; Assignment.new,是分配对象保存?

没有。这正是assignments.build做,没有什么区别。

No. This is exactly what assignments.build does, no difference.

如果我做some_user.assignments&LT;&LT; Assignment.create,是两个数据库调用制作,或只是一个?

刚分配。

什么样,如果我修改创建它,并将其添加之间的分配对象some_user.assignments?

不明白,不好意思。

如果我救会发生什么!一个分配对象,其相应的用户还没有被保存在数据库中?

据保存到数据库中没有为user_id。当你再调用保存您的用户,更新命令发到分配中的用户ID添加。这是在控制台:

It is saved to the database without a user_id. When you then call save on your user, an update command is issued to the assignment to add in the user id. Here it is in console:

u = User.new(:name => "John Doe")
#<User id: nil, name: "John Doe", created_at: nil, updated_at: nil> 

a = Assignment.new(:name => "test")
#<Assignment id: nil, name: "test", user_id: nil, created_at: nil, updated_at: nil> 

u.assignments << a
#[#<Assignment id: nil, name: "test", user_id: nil, created_at: nil, updated_at: nil>] 

a.save!
#SQL (0.2ms)  INSERT INTO `assignments` (`created_at`, `name`, `updated_at`, `user_id`) VALUES ('2012-06-01 19:33:24', 'test', '2012-06-01 19:33:24', NULL)

a.user_id
#nil 

u.save!
#INSERT INTO `users` (`created_at`, `name`, `updated_at`) VALUES ('2012-06-01 19:33:36', 'John Doe', '2012-06-01 19:33:36')
#UPDATE `assignments` SET `user_id` = 3, `updated_at` = '2012-06-01 19:33:36' WHERE `assignments`.`id` = 3

希望这有助于。

Hope this helps.

这篇关于当在的has_many关系活动记录的对象保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆