Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are Active Record objects in has_many relationships saved?

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

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!.

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:

  • 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. 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.

like image 755
Taymon Avatar asked Dec 01 '22 23:12

Taymon


1 Answers

NOTE: All the console tests below are run in Rails 3. You might get a different output in Rails 1, you'll have to run the tests yourself to compare.

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:

If I do some_user.assignments.build, is the Assignment object saved?

Nope

If I do some_user.assignments << Assignment.new, is the Assignment object saved?

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

If I do some_user.assignments << Assignment.create, are two database calls made, or just one?

Just the assignments.

What about if I modify the Assignment object between creating it and adding it to some_user.assignments?

Don't understand, sorry.

What happens if I save! an Assignment object whose corresponding User has not yet been saved in the database?

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.

like image 153
jstrong Avatar answered Dec 06 '22 17:12

jstrong