This project is archived and is in readonly mode.
has_one + validation Object#create_something bug
-
nikz
Hey Oleg,
It's not to do with scoping at all is it?
What does your create_head method look like?
I've run into this one previously I think...
Cheers,
Nik
-
Oleg
create_head is the Rails built-in method. See: http://rails-doc.org/docs/Active...
I have a feeling that something gets messed due to the validation method. I might dig a bit deeper later, but for now I just threw this bug here.
In any case create_* method should not generate UPDATE sql. That makes no sense.
It's Rails 2.1 I didn't see a mention of something like that in tickets so I assume edge has the same issue, probably.
-
nikz
Hey Oleg,
Ah right :) Nope, I don't think it's to do with the bug I ran into before upon closer inspection.
I'm still playing with it (as the fact that you get back an unsaved Head is totally a bug).
The UPDATE query is just to preserve the integrity of the has_one - i.e it's de-associating the old Head. But it looks like it's not saving the new Head.
Hope that helps :)
-
nikz
It looks like Rails isn't saving the second head - the query:
SELECT `chicken_id` FROM `heads` WHERE (`heads`.chicken_id IS NULL AND `heads`.id <> 10)is the validates_uniqueness_of failing :) If you comment that validation out, you'll see that Rails does two INSERTs.
I guess the bug here is that it null-ifys the old Head, even though the new one has failed validation.
The docs say create_whatever should work like;
h = Head.new("chicken_id" => id); h.save; hAnd it is - returning an un-saved Head.
So, I'll write up a patch to make sure it only runs HasOneAssociation#replace if the validations pass? Does that make sense to everyone?
-
nikz
- Tag changed from activerecord to activerecord, edge, patch, tested
Patch against edge attached :)
-
Jack Neto
Hey guys,
It's been a while since this bug showed it's ugly face.
I'm using Rails 2.3.5 and I still have the same problem.
