This project is archived and is in readonly mode.
built polymorphic association incorrectly returns nil for associated record
Reported by Dallas Reedy | August 21st, 2009 @ 01:07 AM
While using a polymorphic association, and when specifically using #build (rather than #new or #create) to initialize the associated record, trying to get back to the parent record gives +nil+:
class Company < ActiveRecord::Base
has_many :addresses, :as => :addressable
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
Then in the console we can test it out:
c = Company.create
c.addresses.build.addressable #=> nil
c.addresses.create.addressable #=> #<Company…>
c.addresses.new.addressable #=> #<Company…>
c.addresses.build(:addressable => c).addressable #=> #<Company…>
Comments and changes to this ticket
-
Rainer Blessing August 9th, 2010 @ 08:32 AM
- Tag changed from association, collection.build, polymorphic, polymorphic_association to association, collection.build, polymorphic, polymorphic_association, resolved
- Importance changed from to
I tested this with Rails 2.3.8 and 3.0.0.RC. Both times c.addresses.build.addressable returned the Company object.
-
Rainer Blessing August 9th, 2010 @ 05:10 PM
- Tag changed from association, collection.build, polymorphic, polymorphic_association, resolved to association, collection.build, polymorphic, polymorphic_association
-
Andrea Campi October 9th, 2010 @ 06:05 PM
-1 I could not reproduce this, does it still happen for you?
-
Stephen Blackstone November 4th, 2010 @ 09:23 PM
I just tried this in Rails 2.3.10 using the exact model in the report and it worked for me..
c = Company.create => # c.addresses.build.addressable => #
-
Stephen Blackstone November 4th, 2010 @ 09:23 PM
c = Company.create
# c.addresses.build.addressable # -
Santiago Pastorino February 5th, 2011 @ 08:56 PM
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 5th, 2011 @ 08:56 PM
- State changed from open to stale
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>