# Building and saving has\_many and has\_one relations

**URL:** <https://www.ruby-forum.com/t/building-and-saving-has-many-and-has-one-relations/104120>\
**Category:** Rails\
**Created:** [May 14, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/building-and-saving-has-many-and-has-one-relations/104120 "2007-05-14T15:16:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [May 14, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/building-and-saving-has-many-and-has-one-relations/104120/1 "2007-05-14T15:16:15Z")

</div>

Hi,

I am very confused about an aspect of has\_one and has\_many relations,  
with regard to using the build method, and saving the belongs\_to side.  
It’s a somewhat long post. so please bear with me 🙂

First let’s consider the has\_one scenario. Let’s say I have a student  
who must own exactly one car:

class Student  
has\_one(:car)  
end

class Car  
belongs\_to(:student)  
validates\_presence\_of(:student\_id)  
end

When I do

student = Student.new(attributes\_of\_student)  
car = student.build\_car(attributes\_of\_car)

The car is created, and I can do:

student.save!  
car.save!

I don’t understand why build\_car also assigns the car to the student.  
According to the docs [1], build is an equivalent, in this case, for:

Car.new(“student\_id” =\> student.id, attributes\_of\_car)

which does it assign the car to the student. Is that simply a bug in  
the docs?

And now comes has\_many, which is where the real confusion begins. Take  
the same scenario, but now a student can own one or more cars:

class Student  
has\_many(:cars)  
end

class Car  
belongs\_to(:student)  
validates\_presence\_of(:student\_id)  
end

When I do

student = Student.new(attributes\_of\_student)  
student.cars.build(attributes\_of\_car1)  
student.cars.build(attributes\_of\_car2)

Until now, everything is fine, except that the cars are part of the  
cars collection of student, which doesn’t happen according to the doc  
[2]. The real problem comes when I do

student.save!

It won’t save when there are invalid cars, and the cars are always  
invalid, because student\_id is still nil: catch 22. Why doesn’t rails  
automatically handle the student relation of car here, like it does  
for the has\_one?

Another problem, is that for the has\_one, I have to manually call  
“valid?” on car, to be able to see invalid fields in the GUI when the  
student also has invalid fields (the call to student.save! aborts the  
block so that the car is not checked), but for the has\_many, calling  
“save!” on student automatically marks all invalid cars, and therefore  
also all the fields in the GUI.

I hope somebody is able to alleviate my confusion.

[1]

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000643)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

[2]

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000642)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.
