Noob: Child records not saved

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!

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/

I think Rails checks the project_contact as saved here:

@project.project_contact.save

So Rails thinks it’s already saved here:

@project.save

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 … :slight_smile: copied to project_contacts.

Thanx a lot

Regards,

Gerard.

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!

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!

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

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 … :wink:

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!

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 … :slight_smile:

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: