# Child object saving fails

**URL:** <https://www.ruby-forum.com/t/child-object-saving-fails/48485>\
**Category:** Rails\
**Created:** [November 21, 2005, 8:44am UTC](https://www.ruby-forum.com/t/child-object-saving-fails/48485 "2005-11-21T08:44:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![azraelnewtype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@azraelnewtype](https://www.ruby-forum.com/u/azraelnewtype)\
**Post date:** [November 21, 2005, 8:44am UTC](https://www.ruby-forum.com/t/child-object-saving-fails/48485/1 "2005-11-21T08:44:51Z")

</div>

This has me pulling out my hair, despite being what’s going to turn out  
to be a trivial problem.

For the sake of argument, let’s say I have two database tables, updates  
and poems. The Update model includes has\_one :poem, and Poem has  
belongs\_to :update. poems the table has update\_id as well, just to be  
clear about how it’s laid out. Also, for the sake of argument I’ll  
mention that poems has a field called title, though there are others.

When I build an update I don’t automatically insert a poem, because  
there isn’t necessarily going to be one per, but there’s an action to  
create a poem later on. This action succeeds, inserting a new row into  
poems with a unique id and linking to the correct update. If, at this  
point, I set the title, it will save to the database as well. However,  
once that object is attached to update, no amount of editing and saving  
@update.poem.save will ever do anything to the database. Editing the  
database directly will cause the page to return the correct values, so  
it is clearly looking in the right place, but save just won’t work.

To make matters worse, or at least more maddening for me, throwing the  
save statement into an if statement will always return true, despite not  
actually saving anything. I’m sure I’m overlooking something stupid.  
Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![azraelnewtype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@azraelnewtype](https://www.ruby-forum.com/u/azraelnewtype)\
**Post date:** [November 21, 2005, 9:23am UTC](https://www.ruby-forum.com/t/child-object-saving-fails/48485/2 "2005-11-21T09:23:41Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 20, 2005, at 11:44 PM, Chris Eads wrote:

> database directly will cause the page to return the correct values, so  
> it is clearly looking in the right place, but save just won’t work.
> 
> To make matters worse, or at least more maddening for me, throwing the  
> save statement into an if statement will always return true,  
> despite not  
> actually saving anything. I’m sure I’m overlooking something stupid.  
> Any help would be greatly appreciated.

update is a method of ActiveRecord::Base so you can’t use it as  
a name for your association. You may name it differently:  
has\_one :parent, :class\_name =\> ‘Update’  
but it may be clearer to rethink the term Update; perhaps a more  
specific term such as Post would be appropriate.

I’m sorry this error has caused you to pull your hair out. Rest  
assured, we are working to put it all back in. Rails should cry  
out when you use an existing method name and, better, it shouldn’t  
snatch up generic method names in the first place.

This is assuming, of course, that the name ‘update’ was not chosen  
purely for the sake of argument 🙂

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDgYOwAQHALep9HFYRAsOYAJ9n6hAViIG8Bjppgw7kvbWzOMUQHwCdHASn  
hkYBlKQuGI+1vOLbQ2N3uTI=  
=SXl6  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![azraelnewtype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@azraelnewtype](https://www.ruby-forum.com/u/azraelnewtype)\
**Post date:** [November 21, 2005, 6:15pm UTC](https://www.ruby-forum.com/t/child-object-saving-fails/48485/3 "2005-11-21T18:15:07Z")

</div>

jeremy wrote:

> update is a method of ActiveRecord::Base so you can’t use it as  
> a name for your association. You may name it differently:  
> has\_one :parent, :class\_name =\> ‘Update’  
> but it may be clearer to rethink the term Update; perhaps a more  
> specific term such as Post would be appropriate.
> 
> I’m sorry this error has caused you to pull your hair out. Rest  
> assured, we are working to put it all back in. Rails should cry  
> out when you use an existing method name and, better, it shouldn’t  
> snatch up generic method names in the first place.
> 
> This is assuming, of course, that the name ‘update’ was not chosen  
> purely for the sake of argument 🙂

I never referenced update in any way it could have been mistaken for the  
method of ActiveRecord::Base (always used @update as the object), but  
just to be safe I did in fact change the table, the class, and  
everything that references it to Post (or the correct mutation thereof).  
The behavior is identical.

Any other ideas?

---

<div class="post-metadata">

**Author:** ![azraelnewtype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@azraelnewtype](https://www.ruby-forum.com/u/azraelnewtype)\
**Post date:** [November 21, 2005, 6:53pm UTC](https://www.ruby-forum.com/t/child-object-saving-fails/48485/4 "2005-11-21T18:53:32Z")

</div>

just as a test i did:

class User \< ActiveRecord::Base  
has\_one :poem  
end

class Poem \< ActiveRecord::Base  
belongs\_to :user  
end

where the tables are:

## users

id  
name

## poems

id  
title  
body  
user\_id

then in console, i did:

user = User.create(:name =\> “Bob”)  
poem = Poem.create(:title =\> “My Poem”, :body =\> “this is my poem”,  
:user\_id  
=\> user.id [http://user.id](http://user.id))  
user.poem.title (outputs “My Poem”)  
user.poem.title = “My New Poem Title”  
user.poem.save  
user.poem.reload  
user.poem.title (outputs “My New Poem TItle”)

ok, so that works

now trying to duplicate your error:

user = User.find\_by\_name(“Bob”)  
user.poem.title (outputs “My New Poem TItle”)  
user.poem.title = “My Really New Poem Title”  
user.poem.save  
user.poem.reload  
user.poem.title (outputs “My Really New Poem TItle”)

everything works fine so all I can say is that there must be something  
you  
are (not) doing in your code.

perhaps pasting the portion of the code that is giving you trouble will  
help.

Chris
