views:

4016

answers:

2

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
   :has_many :tags
   :belongs_to :person
end

Class Person
  :has_many :dogs
end

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_to relationship.

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

+6  A: 

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

d = Tag.new
d.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!)

BushyMark
Great! Thank you very much for your answer.
rube_noob
A: 

BushyMark is dead on...just wanted to add this link (he refers to it, and I found it very helpful in answering this and similar questions): http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Brennon Bortz