This project is archived and is in readonly mode.
Inconsistent behavior of collection.new vs collection.build
Reported by Szymon Nowak | March 24th, 2011 @ 07:42 PM
I'm not sure how exactly collection.new should behave, as it's not really documented (at least not here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassM..., but it's behavior is inconsistent:
class User < ActiveRecord::Base has_many :friendships has_many :friends, :through => :friendships end class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => "User" end user.friendships.new user.friendships # =>  user.friendships.build user.friendships # => [#<Friendship id: nil, user_id: 1, friend_id: nil>] # but user.friends # =>  user.friends.new user.friends # => [#<User id: nil...>]
I'm not sure how it should behave, but it would be great if it always behaved like for non-through associations. It works like this in Rails 3.0.5 and master.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Tickets have moved to Github
The new ticket tracker is available at https://github.com/rails/rails/issues