Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Polymorphic belongs_to association isn't fully populated during build

#3148

With a one-to-many polymorphic association, the build method doesn't work as expected. Given the following models:

class User < ActiveRecord::Base
  has_many :addresses, :as => :addressable
end

class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true
end

building a new address through the user's has_many association properly populates the addressable_id and addressable_type columns, but returns nil for addressable. Using "new" rather than "build" returns the user as expected.

>> user = User.create(:name => "Steve")
=> #<User id: 1, name: "Steve">
>> new_address = user.addresses.new
=> #<Address id: nil, addressable_id: 1, addressable_type: "User">
>> new_address.addressable
=> #<User id: 1, name: "Steve">
>> built_address = user.addresses.build
=> #<Address id: nil, addressable_id: 1, addressable_type: "User">
>> built_address.addressable
=> nil

Reported by laserlemon · September 4th, 2009 @ 04:33 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 6th, 2010 @ 06:43 AM

  2. bingbing