# Noob: Child records not saved

**URL:** <https://www.ruby-forum.com/t/noob-child-records-not-saved/60730>\
**Category:** Rails\
**Created:** [January 10, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730 "2006-01-10T13:19:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/1 "2006-01-10T13:19:06Z")

</div>

Hi all,

happily coding along, but it seems a belongs\_to record is not saved.

# Create some stuff

def create\_project  
@contact = Contact.new  
@project = Project.new(@params[‘project’])  
@project\_contact = ProjectContact.new(@params[‘collect’])

```
@project.project_contact = @project_contact
contact = Contact.find(@params['id']['collect'])

```

# Copy fields

```
@project.project_contact['firstname'] = contact["firstname"]
@project.project_contact['lastname'] = contact["lastname"]

```

# The above two vars are set, checked it with “breakpoint”

# But not saved here!!!

```
@project.project_contact.save

if @project.save
	flash[:notice] ... etc ...

```

Is this solved with a do loop, for all attribs? Shouldn’t AR take care  
of  
this? What am I missing?

Thnx n Grtz

Gerard

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/2 "2006-01-10T16:44:10Z")

</div>

On Jan 10, 2006, at 5:17 AM, Gerard wrote:

> @project.project\_contact = @project\_contact
> 
> if @project.save  
> flash[:notice] … etc …

If I understand correctly a ‘project’ has\_one ‘project contact’,  
right? If that’s the case, then you’re attempting to save the  
project contact before @project has an ID, and thus there’s no way to  
associate it with the project. Remember that in order to save a  
belongs\_to record, the ID of the object to which it belongs must be  
known at the time you are saving.

Try removing the @project.project\_contact.save line and relying on  
the @project.save call. It’ll save the project and its associated  
project\_contacts automatically since they are new (unsaved) objects.

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/3 "2006-01-10T17:09:24Z")

</div>

I think Rails checks the project\_contact as saved here:

@project.project\_contact.save

So Rails thinks it’s already saved here:

@project.save

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/4 "2006-01-10T17:14:28Z")

</div>

Duane,

Replies inline …

On Tuesday 10 January 2006 16:42, Duane J. tried to type something  
like:

> If I understand correctly a ‘project’ has\_one ‘project contact’,  
> right? If that’s the case, then you’re attempting to save the  
> project contact before @project has an ID, and thus there’s no way to  
> associate it with the project. Remember that in order to save a  
> belongs\_to record, the ID of the object to which it belongs must be  
> known at the time you are saving.  
> Makes sense. Thanks for pointing that out.

> Try removing the @project.project\_contact.save line and relying on  
> the @project.save call. It’ll save the project and its associated  
> project\_contacts automatically since they are new (unsaved) objects.

> > ```
> > @project.project_contact['firstname'] = contact["firstname"]
> > @project.project_contact['lastname'] = contact["lastname"]
> > 
> > ```

After these two, as suggested, straight to an explicit:

```
@project.save

```

If I understand you correctly. This one is not needed what soever?

```
@project.project_contact.save

```

Unfortunately, no luck. The id’s get picked up though. In the  
project\_contacts  
table the fields “id” and “project\_id” are filled as they should.

This is the model info btw:

company (hm) ↔ (bt) contacts

project (ho) ↔ (bt) project\_contacts

As you probably figured out from the code. Some fields from contacts are  
(well … need to be … 🙂 copied to project\_contacts.

Thanx a lot

Regards,

Gerard.

> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> –  
> “Who cares if it doesn’t do anything? It was made with our new  
> Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/5 "2006-01-10T17:59:44Z")

</div>

Jules / Duane,

@Jules: I tried it with the one first save disabled, but no luck.

This is a breakpoint snap from @project, which tells something.

=\> #\<Project:0x40bf22b0 @new\_record=false,  
@project\_contact=#\<ProjectContact:0x40bf07a8 @new\_record=false,  
@errors=#\<ActiveRecord::Errors:0x40be2ef0 @errors={},  
@base=#\<ProjectContact:0x40bf07a8 …\>\>, @attributes={“project\_id”=\>60,  
“company\_name”=\>"", “id”=\>29, “lastname”=\>“van dattum”,  
“firstname”=\>“saartje”, “first\_name”=\>"", “last\_name”=\>""}\>,  
@errors=#\<ActiveRecord::Errors:0x40be8544 @errors={},  
@base=#\<Project:0x40bf22b0 …\>\>, @new\_record\_before\_save=false,  
@attributes={“project\_description”=\>“daaro”, “id”=\>60,  
“project\_name”=\>“waaro”}\>

The interesting part is this.

@attributes={“project\_id”=\>60, “company\_name”=\>"", “id”=\>29,  
“lastname”=\>“van  
dattum”, “firstname”=\>“saartje”, “first\_name”=\>"", “last\_name”=\>""}

First name and last name are in there twice. Once with and once without  
the  
underscore. But I’m afraid I don’t know which stands for which. The  
table  
names are, with underscores as Rails expects them.

Regards.

Gerard.

On Tuesday 10 January 2006 17:09, Jules J. tried to type something  
like:

> I think Rails checks the project\_contact as saved here:
> 
> @project.project\_contact.save
> 
> So Rails thinks it’s already saved here:
> 
> @project.save

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/6 "2006-01-10T19:14:55Z")

</div>

I don’t know what you’re trying to do…

Maybe a single table contacts, like this:

Contact

- name
- email
- phone
- etc

Project has\_and\_belongs\_to\_many :contacts  
Company has\_and\_belongs\_to\_many :contacts

Then you can (probably) do things like:

@project.contacts = @company.contacts

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/7 "2006-01-10T22:29:34Z")

</div>

Jules,

I’m not going to drown you in code. I’ve got the feeling that I’m making  
things to complicated anyway. I’m about to post another Q for advise on  
this.

Not to leave you in the dark (yeah like my code is the light … ;-), my  
‘thing’ in a nutshell.

company (hm) \<-\> (bt) contacts  
project (ho) \<-\> (bt) project\_contacts

project (ho) \<-\> (bt) project\_products

I’m at the point that i’m able to copy some fields (while creating a new  
project) from contacts to project\_contacts. (I do this, so that when a  
contacts is removed (e.g. resign or other job) the project data is still  
accessible. Within this step I also would like to obtain the name of the  
company the contact belongs to. So during the create project (and some  
instance variables within that action.This is the real field copy step:

```
@project.project_contact.last_name = contact.last_name

```

The next step would be something like

```
@project.project_contact.company_name = contact.company.name

```

But here is where the lights went out. I’ve got the feeling that it  
either all  
can be made much simpler, or that I want to much. Although from within  
the  
app, it’s all very easy useable.

I’ll probably put part of this email in the next post. Feedback is  
welcome,  
but you shouldn’t leave any sleep over it … 😉

Thanx n Regards,

Gerard.

On Tuesday 10 January 2006 19:14, Jules J. tried to type something  
like:

> Project has\_and\_belongs\_to\_many :contacts  
> Company has\_and\_belongs\_to\_many :contacts
> 
> Then you can (probably) do things like:
> 
> @project.contacts = @company.contacts

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/noob-child-records-not-saved/60730/8 "2006-01-10T23:01:56Z")

</div>

Guys,

A friggin’ typo. It was the difference in column names in the database.  
Leaves  
me with another discussion. Would I consider “first name” one or to  
words …  
@-)

No seriously. It gets tricky now. The source of the copied fname and  
lname the  
contact. Has a belongs\_to to a table called company. Of which I also  
want the  
name.

Don’t need you to work for me … 🙂

But should I then invoke a @company = Company.new to end up with  
something  
like

@project.project\_contact[“company\_name”] = contact.company[‘name’]

Anyway … Thanx a lot for your brainpower.

Regards,

Gerard

On Tuesday 10 January 2006 17:27, Gerard tried to type something like:
