#191 √ resolved
Tim Harper

Belongs to polymorphic association assignment with new records doesn't update foreign id or type

Reported by Tim Harper | May 14th, 2008 @ 02:33 AM

Patch on github:

http://github.com/timcharper/rai...

Fixes the following scenarios:

  • I have validates_inclusion_of on the type field for a polymorphic

belongs_to association. I assign a new record to the model's polymorphic

relationship of the proper type. validation fails because the type field

has not been updated.

  • I replace the value for a polymorphic association to a new record of

another class. The type field still says its the previous class, and the

id field points to the previous record as well.

-----

[7:22pm] technoweenie: timcharper: it probably shouldnt set the record.id if record is new
[7:22pm] technoweenie: i'm not sure what record.id will be, nil I guess?  maybe it doesnt matter
[7:22pm] timcharper: yeah - that's what I'm thinking
[7:22pm] timcharper: it should at least clear it
[7:22pm] technoweenie: i guess in rare cases someone might pre-set the id  and would want it set
[7:22pm] timcharper: i was debating between that or a ternary
[7:23pm] technoweenie: record.new_record? ? nil : record.id
[7:23pm] timcharper: yeah
[7:23pm] timcharper: but was thinking the case would be in the minority that they set the id before saving the record
[7:23pm] timcharper: and if so, they probably did so intentionally, and would likely want the foreign key to receive that value as well
[7:24pm] technoweenie: yup
[7:24pm] technoweenie: as long as it doesnt blow up for the rest of us
[7:24pm] technoweenie: blow up, or set bad values
[7:25pm] timcharper: can you think of how it would ?  I can't think of anything
[7:25pm] technoweenie: nope
[7:25pm] technoweenie: i guess if you're a moron and try to set it wrong, you deserve what you get
[7:26pm] timcharper: yeah, that sounds about right
[7:26pm] timcharper: submitting the ticket
[7:26pm] technoweenie: and if its committed, and there is some wacky case where it blows up, it just means we were missing a regression test anyway

Comments and changes to this ticket

  • rick

    rick May 14th, 2008 @ 02:34 AM

    • → Milestone changed from “” to “2.1.1”
    • → State changed from “new” to “open”
    • → Assigned user changed from “” to “rick”
  • rick

    rick May 20th, 2008 @ 06:39 PM

    • → Milestone cleared.
    • → State changed from “open” to “new”
    • → Title changed from “Belongs to polymorphic association assignment with new_records doesn't update ” to “Belongs to polymorphic association assignment with new records doesn't update foreign id or type”
  • rick

    rick May 20th, 2008 @ 06:40 PM

    • → State changed from “new” to “open”
  • Tim Harper

    Tim Harper May 30th, 2008 @ 01:06 AM

    Is this patch not going to make it into 2.1 ?

  • Repository

    Repository May 31st, 2008 @ 09:40 PM

    (from [0580b31b36c0f7dd1a0f8bdd1b1806e3bd65b22d]) belongs_to polymorphic association assignments update the foreign_id and foreign_type fields regardless of whether the record being assigned is new or not.

    fixes the following scenarios:

    • I have validates_inclusion_of on the type field for a polymorphic belongs_to association. I assign a new record to the model's polymorphic relationship of the proper type. validation fails because the type field has not been updated.
    • I replace the value for a ppolymorphic association to a new record of another class. The type field still says its the previous class, and the id field points to the previous record as well.

    [#191 state:closed]

    http://github.com/rails/rails/co...

  • rick

    rick May 31st, 2008 @ 11:03 PM

    • → State changed from “open” to “resolved”

    Uh weird, why wasn't this closed

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

People watching this ticket