This project is archived and is in readonly mode.

#6772 new
Brian Buchalter

Polymorphic type fields not set in Rails 3.1.0.beta1 with Identity Map enabled

Reported by Brian Buchalter | May 10th, 2011 @ 08:58 PM

I believe the identity map functionality of Rails 3.1.0.beta1 has broken polymorphic associations.

See below for steps to recreate


config.active_record.identity_map = true
ActiveRecord::IdentityMap.enabled = true


class CreatePhysicalAddresses < ActiveRecord::Migration
  def change
    create_table :physical_addresses do |t|
      t.string :line1
      t.string :line2
      t.string :city
      t.integer :state_id
      t.string :zipcode
      t.integer :county_id
      t.string :latlong
      t.references :physical_addressable, :polymorphic => true
    add_index :physical_addresses, [:physical_addressable_id, :physical_addressable_type], :name => "physical_addressable"
    add_index :physical_addresses, :state_id
    add_index :physical_addresses, :county_id

class CreateLands < ActiveRecord::Migration
  def self.up
    create_table :lands do |t|
      t.string :tax_map_reference
      t.string :plat_number
      t.text :location_comments
      t.text :adverse_environmental_conditions
      t.integer :created_by
      t.integer :updated_by
      t.integer :land_special_ownership_type_id
    add_index :lands, :land_special_ownership_type_id

  def self.down
    drop_table :lands


class Land < ActiveRecord::Base
  has_one :address, :as => :physical_addressable, :dependent => :destroy, :class_name => "PhysicalAddress"
  accepts_nested_attributes_for :address, :allow_destroy => true
  attr_accessible :address_attributes
  delegate :latlong, :to => :address, :prefix => true, :allow_nil => true
  validates_associated :address

class PhysicalAddress < ActiveRecord::Base
  attr_accessible :line1, :line2, :city, :state_id, :zipcode, :county_id, :latlong
  belongs_to :physical_addressable, :polymorphic => true
  belongs_to :state
  delegate :name, :to => :state, :prefix => true, :allow_nil => true
  belongs_to :county
  delegate :name, :to => :county, :prefix => true, :allow_nil => true

  validates_presence_of :line1, :city, :state_id
  validates_length_of :zipcode, :is => 5

Console output:

Loading development environment (Rails 3.1.0.beta1)
ruby-1.9.2-p180 :001 > Land.create({"address_attributes"=>{"line1"=>"10300 Little Patuxent Parkway", "line2"=>"", "city"=>"Columbia", "state_id"=>"1", "zipcode"=>"21044", "county_id"=>"14"}})
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `lands` (`adverse_environmental_conditions`, `created_at`, `created_by`, `land_special_ownership_type_id`, `location_comments`, `plat_number`, `tax_map_reference`, `updated_at`, `updated_by`) VALUES (NULL, '2011-05-10 19:50:11', NULL, NULL, NULL, NULL, NULL, '2011-05-10 19:50:11', NULL)
  SQL (0.2ms)  INSERT INTO `physical_addresses` (`city`, `county_id`, `created_at`, `latlong`, `line1`, `line2`, `physical_addressable_id`, `physical_addressable_type`, `state_id`, `updated_at`, `zipcode`) VALUES ('Columbia', 14, '2011-05-10 19:50:11', NULL, '10300 Little Patuxent Parkway', '', 4, NULL, 1, '2011-05-10 19:50:11', '21044')
   (3.6ms)  COMMIT
 => #<Land id: 4, tax_map_reference: nil, plat_number: nil, location_comments: nil, adverse_environmental_conditions: nil, created_by: nil, updated_by: nil, land_special_ownership_type_id: nil, created_at: "2011-05-10 19:50:11", updated_at: "2011-05-10 19:50:11">
ruby-1.9.2-p180 :002 > Land.find(4).address
  Land Loaded  From Identity Map (id: 4)
 => #<PhysicalAddress id: 4, line1: "10300 Little Patuxent Parkway", line2: "", city: "Columbia", state_id: 1, zipcode: "21044", county_id: 14, latlong: nil, physical_addressable_id: 4, physical_addressable_type: nil, created_at: "2011-05-10 19:50:11", updated_at: "2011-05-10 19:50:11">

Note that the SQL INSERT into physical addresses does not include a physical_addressable_type. Worked just fine in Rails 3.0.7.

The address is loaded from the identity map, but if I exit console and try to access the address record again, it is not available:

Loading development environment (Rails 3.1.0.beta1)
ruby-1.9.2-p180 :001 > Land.find(4).address
  Land Load (0.6ms)  SELECT `lands`.* FROM `lands` WHERE `lands`.`id` = 4 LIMIT 1
  PhysicalAddress Load (0.4ms)  SELECT `physical_addresses`.* FROM `physical_addresses` WHERE `physical_addresses`.`physical_addressable_id` = 4 AND `physical_addresses`.`physical_addressable_type` = 'Land' LIMIT 1
 => nil

Comments and changes to this ticket

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