This project is archived and is in readonly mode.
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
-
Staszek July 15th, 2010 @ 10:59 AM
- Tag changed from associations, association_collection, belongs_to, hasmany to 2.x, associations, association_collection, belongs_to, hasmany
-
Subba July 15th, 2010 @ 12:50 PM
you should try using inverse_of option as shown below.
class User has_many :movies, :inverse_of => :user end class Movie belongs_to :user, :inverse_of => :movies end
-
Staszek July 15th, 2010 @ 01:16 PM
Thank you. It looks like after https://rails.lighthouseapp.com/projects/8994/tickets/3533 documentation should be updated. Rdocs for 2.3.8 does not have any information about this option
-
Neeraj Singh July 15th, 2010 @ 01:41 PM
- State changed from new to resolved
- Importance changed from to Low
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>