This project is archived and is in readonly mode.

#5118 ✓resolved
Staszek

Not saved children does not have parent object in has many association

Reported by Staszek | July 15th, 2010 @ 10:58 AM

We have got 2 objects in has_many relation

class User
 has_many :movies
end
class Movie
 belongs_to :user
end

If you create a parent object and the children then you have not access to parent object from children until you saved it.

user = User.new
movie = Movie.new
user.movies << movie

user.movies
>> [#<Movie id: nil>]
movie.user
>> nil

In my opinion children should always have information about its parent. It can be usefull if you need this information to prepare object before saving(or in validations).

Comments and changes to this ticket

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>

Pages