Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
bbuchalter opened this Issue · 8 comments

4 participants

@bbuchalter

I believe the identity map functionality of Rails 3.1.0.beta1 has broken polymorphic associations. This issue cross references Lighthouse ticket #6772 (https://rails.lighthouseapp.com/projects/8994/tickets/6772).

See below for steps to recreate

Application.rb

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

Migrations:

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
      t.timestamps
    end
    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
  end
end

class CreateLands < ActiveRecord::Migration
  def change
    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
      t.timestamps
    end
    add_index :lands, :land_special_ownership_type_id
  end
end

Models:

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
end

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
end

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
@josevalim
Owner
@bbuchalter
@bbuchalter
@bbuchalter

Jose, could you tell me more about the issue you're referring to in LH? Which ticket? Was it the one I submitted?

@josevalim
Owner

I have just checked the ticket but it was actually another issue, check #403. One question, does this bug happen on Rails 3.0.x?

@bbuchalter
@jonleighton jonleighton was assigned
@miloops

Hey bbuchalter, I reproduced the issue in beta1, but it seems to be fixed in master. I ran exactly the same migrations and models you posted and it should be fixed now.

If you find it still fails on master for you please do comment again.

@josevalim josevalim closed this
@bbuchalter

Great work guys! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.