#516 stale
Oleg

has_one + validation Object#create_something bug

Reported by Oleg | June 30th, 2008 @ 08:11 PM | in 2.x

Odd one I found today:


class Chicken
  has_one :head
end

class Head
  belongs_to :chicken
  validates_uniqueness_of :chicken_id
end


>> @chicken = Chicken.first
>> @chicken.create_head 
=> #<Head id: 10, chicken_id: 1>

# second create should trigger validation error, or at least do nothing. Instead we get this:

>> @chicken.create_head 
=> #<Head id: nil, chicken_id: 1>

For some reason second call to create_head generates SQL similar to this:

SELECT `chicken_id` FROM `heads` WHERE (`heads`.chicken_id = 1)

SELECT `chicken_id` FROM `heads` WHERE (`heads`.chicken_id IS NULL AND `heads`.id <> 10)

UPDATE `heads` SET `chicken_id` = NULL, WHERE `id` = 10

Doesn't make much sense to me

Comments and changes to this ticket

  • nikz

    nikz June 30th, 2008 @ 11:39 PM

    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

    Oleg July 1st, 2008 @ 12:45 AM

    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

    nikz July 5th, 2008 @ 05:15 AM

    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

    nikz July 5th, 2008 @ 05:37 AM

    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; h
    

    And 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

    nikz July 6th, 2008 @ 05:12 AM

    • → Tag changed from “activerecord” to “activerecord edge patch tested”

    Patch against edge attached :)

  • Joshua Peek

    Joshua Peek October 28th, 2008 @ 04:21 PM

    • → State changed from “new” to “stale”

    Staling out, please reopen if this is still a problem.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins