This project is archived and is in readonly mode.

#1469 ✓duplicate
Paul Mason

Changing foreign key doesn't set relationship as dirty

Reported by Paul Mason | November 25th, 2008 @ 04:29 PM | in 2.x

When changing a foreign key on a belongs_to relationship, the relationship is not set to dirty.

If I have a model with a relationship to a country, and I change the country_id using attributes=({:country_id => X}) or model.country_id=X, and then access the relationship, it uses the cached country object, instead of looking up the new country_id.

Comments and changes to this ticket

  • Darren

    Darren December 8th, 2008 @ 07:27 PM

    I have come across a problem which I believe is closely related to this.

    If I have a model that belongs_to another, and requires the presence of the relationship (i.e., a valid non-null foreign key), I can get AR to save invalid records in the database.

    For example, with this...

    Class Parent < AR::B
    class Child < AR::B
      belongs_to :parent # using parent_id
      validates_presence_of :parent
    # child =
    # child.valid? # false
    # child.parent_id = # a valid parent
    # child.valid? # true
    # child.parent_id = 9999 # doesn't exist
    # child.valid? # true
    #   # true
    # child.reload
    # child.parent_id # 9999
    # child.parent    # nil
    # child.valid? # false
  • Frederick Cheung

    Frederick Cheung December 21st, 2008 @ 11:11 AM

    • State changed from “new” to “duplicate”

    Duplicate of #142

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket