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.

build through an association does not set the back-reference in Rails 3.0.0

#4269

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

Reported by Martin Plöger · March 25th, 2010 @ 09:46 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Evgeniy Dolzhenko
    Evgeniy Dolzhenko

    Make sure you're setting :inverse_of option on your associations. Additional details in this ticket #2815 nested models: build should directly assign the parent

    March 26th, 2010 @ 05:50 AM

  2. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to invalid

    March 27th, 2010 @ 07:38 AM