Forum: Ruby on Rails Noob: Child records not saved

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-10 13:19
(Received via mailing list)
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!
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 Duane Johnson (Guest)
on 2006-01-10 16:44
(Received via mailing list)
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 Johnson
(canadaduane)
http://blog.inquirylabs.com/
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-10 17:09
I think Rails checks the project_contact as saved here:

@project.project_contact.save

So Rails thinks it's already saved here:

@project.save
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-10 17:14
(Received via mailing list)
Duane,

Replies inline ...

On Tuesday 10 January 2006 16:42, Duane Johnson 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
--
"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!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-10 17:59
(Received via mailing list)
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 Jacobs 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!
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-10 19:14
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
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-10 22:29
(Received via mailing list)
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 Jacobs 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!
Cebd93ec030e868ed2786963ebc141ad?d=identicon&s=25 Gerard Petersen (Planet) (Guest)
on 2006-01-10 23:01
(Received via mailing list)
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:
This topic is locked and can not be replied to.