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

Closed
bbuchalter opened this Issue May 12, 2011 · 8 comments

Comments

Projects
None yet
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

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim May 12, 2011

Contributor

Thanks for the report! I assume you have tried the above without identity map and it worked? Because the issue mentioned in LH was ported to Github and the conclusion was that we couldn't do anything.

Contributor

josevalim commented May 12, 2011

Thanks for the report! I assume you have tried the above without identity map and it worked? Because the issue mentioned in LH was ported to Github and the conclusion was that we couldn't do anything.

@bbuchalter

This comment has been minimized.

Show comment
Hide comment
@bbuchalter

bbuchalter May 12, 2011

You know, I had been thinking about that. I haven't verified it works with
IDmap turned off. I will and update.

On Thu, May 12, 2011 at 8:54 AM, josevalim <
reply@reply.github.com>wrote:

Thanks for the report! I assume you have tried the above without identity
map and it worked? Because the issue mentioned in LH was ported to Github
and the conclusion was that we couldn't do anything.

Reply to this email directly or view it on GitHub:
#526 (comment)

"Who is the happier man, he who has braved the storm of life and lived, or
he who has stayed securely on shore and merely existed?" - HST
"In the long run, you could die tomorrow." - LS

You know, I had been thinking about that. I haven't verified it works with
IDmap turned off. I will and update.

On Thu, May 12, 2011 at 8:54 AM, josevalim <
reply@reply.github.com>wrote:

Thanks for the report! I assume you have tried the above without identity
map and it worked? Because the issue mentioned in LH was ported to Github
and the conclusion was that we couldn't do anything.

Reply to this email directly or view it on GitHub:
#526 (comment)

"Who is the happier man, he who has braved the storm of life and lived, or
he who has stayed securely on shore and merely existed?" - HST
"In the long run, you could die tomorrow." - LS

@bbuchalter

This comment has been minimized.

Show comment
Hide comment
@bbuchalter

bbuchalter May 12, 2011

Looks like the issue happens regardless of whether id map is enabled or
not....

On Thu, May 12, 2011 at 9:14 AM, Brian Buchalter bal711@gmail.com wrote:

You know, I had been thinking about that. I haven't verified it works with
IDmap turned off. I will and update.

On Thu, May 12, 2011 at 8:54 AM, josevalim <
reply@reply.github.com>wrote:

Thanks for the report! I assume you have tried the above without identity
map and it worked? Because the issue mentioned in LH was ported to Github
and the conclusion was that we couldn't do anything.

Reply to this email directly or view it on GitHub:
#526 (comment)

"Who is the happier man, he who has braved the storm of life and lived, or
he who has stayed securely on shore and merely existed?" - HST
"In the long run, you could die tomorrow." - LS

"Who is the happier man, he who has braved the storm of life and lived, or
he who has stayed securely on shore and merely existed?" - HST
"In the long run, you could die tomorrow." - LS

Looks like the issue happens regardless of whether id map is enabled or
not....

On Thu, May 12, 2011 at 9:14 AM, Brian Buchalter bal711@gmail.com wrote:

You know, I had been thinking about that. I haven't verified it works with
IDmap turned off. I will and update.

On Thu, May 12, 2011 at 8:54 AM, josevalim <
reply@reply.github.com>wrote:

Thanks for the report! I assume you have tried the above without identity
map and it worked? Because the issue mentioned in LH was ported to Github
and the conclusion was that we couldn't do anything.

Reply to this email directly or view it on GitHub:
#526 (comment)

"Who is the happier man, he who has braved the storm of life and lived, or
he who has stayed securely on shore and merely existed?" - HST
"In the long run, you could die tomorrow." - LS

"Who is the happier man, he who has braved the storm of life and lived, or
he who has stayed securely on shore and merely existed?" - HST
"In the long run, you could die tomorrow." - LS

@bbuchalter

This comment has been minimized.

Show comment
Hide comment
@bbuchalter

bbuchalter May 12, 2011

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

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

@josevalim

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim May 12, 2011

Contributor

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?

Contributor

josevalim commented May 12, 2011

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

This comment has been minimized.

Show comment
Hide comment
@bbuchalter

bbuchalter May 12, 2011

The bug does not appear in rails 3.0.7. I will post a sample app to
recreate the problem on github later today and provide link in this
thread.

Sent from my iPhone

On May 12, 2011, at 12:24 PM, josevalim
reply@reply.github.com
wrote:

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?

Reply to this email directly or view it on GitHub:
#526 (comment)

The bug does not appear in rails 3.0.7. I will post a sample app to
recreate the problem on github later today and provide link in this
thread.

Sent from my iPhone

On May 12, 2011, at 12:24 PM, josevalim
reply@reply.github.com
wrote:

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?

Reply to this email directly or view it on GitHub:
#526 (comment)

@ghost ghost assigned jonleighton May 12, 2011

@miloops

This comment has been minimized.

Show comment
Hide comment
@miloops

miloops May 17, 2011

Contributor

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.

Contributor

miloops commented May 17, 2011

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 May 17, 2011

@bbuchalter

This comment has been minimized.

Show comment
Hide comment
@bbuchalter

bbuchalter May 17, 2011

Great work guys! Thank you!

Great work guys! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment