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.
Comments and changes to this ticket
-
Szymon Nowak March 24th, 2011 @ 07:51 PM
Turns out that
HasManyThroughAssociation
has a simple alias:alias_method :new, :build
, whichHasManyAssociation
doesn't have. Not sure if it's on purpose.
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>