This project is archived and is in readonly mode.
build through an association does not set the back-reference in Rails 3.0.0
Reported by Martin Plöger | March 25th, 2010 @ 09:46 PM
When I call the #build-method on an association the reference back (belongs_to) in the newly created instance is still nil.
@active_record_model.other_models.build.active_record_model # => nil
This makes it difficult when there are validations on the built
object like 'validates_presence_of :active_record_model'.
These fail it this scenario:
@active_record_model.other_models.build
@active_record_model.save # => false
You have to use a workaround like that:
@active_record_model.other_models.build :active_record_model => @active_record_model
@active_record_model.save # => true
Comments and changes to this ticket
-
Evgeniy Dolzhenko March 26th, 2010 @ 05:50 AM
Make sure you're setting
:inverse_of
option on your associations. Additional details in this ticket https://rails.lighthouseapp.com/projects/8994/tickets/2815-nested-m... -
Yehuda Katz (wycats) March 27th, 2010 @ 07:38 AM
- State changed from new to invalid
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>