
Save/Create fails to work when dealing with belongs_to relationships from inside model
Reported by Aadi Deshpande | July 7th, 2008 @ 05:40 PM
It seems that when trying to create model entities from within a model itself fails when dealing with "belongs_to" type entities.
The attached spec shows 2 examples:
In the first, spawn_evil_twin, i attempt to reuse attributes from my model to create the new ,model.
In that scenario, the foreign key relationship(s) fail(s) to be saved, leaving both FK entities as nil.
In the second is a hack, where I "re-get" the model using the local attribute's id, which does save properly.
Is this a bug? Is there a rationale for this?
I'm not sure if this has anything to do with the IdentityMap not being primed properly, but seems like it should work as expected.
Thanks,
-a
Comments and changes to this ticket
-
Aadi Deshpande July 8th, 2008 @ 12:45 AM
I"ve tried digging a bit more and it seems that the first way doesn't set the dirty flags properly, which is why they never get propagated to the database
Any pointers on where I can look to figure out why that is?
-
Aadi Deshpande July 8th, 2008 @ 10:46 AM
Ok, partly figured this out.
This is because when you reference class.property ( if it's a many-to-1 ) , it's not of a model type, it's actually a ManyToOne::Proxy, which, when the Property tries to get! the key value, returns nil.
A simple fix, which seems to work basically overrides instance_variable_get to be :
def instance_variable_get( var )
super || parent.instance_variable_get( var )
end
-
-
Aadi Deshpande July 8th, 2008 @ 11:05 AM
- no changes were found...
-
Aadi Deshpande July 8th, 2008 @ 11:05 AM
also, here's the patch , tested against edge-dm it causes no regressions.
-
Aadi Deshpande July 8th, 2008 @ 11:58 AM
uploaded new spec, with dm-spec helper, fails in edge with all 3 supported db
-
Dan Kubb (dkubb) December 1st, 2008 @ 01:51 AM
- State changed from new to resolved
- Assigned user cleared.
This has already been updated in sam/dm-core:
http://github.com/sam/dm-core/tr...
Also, dkubb/dm-core has factored out the ManyToOne::Proxy object altogether, so this won't be an issue there either.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
People watching this ticket
- Nobody is watching this ticket.