Child object saving fails

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.

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

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

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

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

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?

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)
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