This project is archived and is in readonly mode.

#6618 new
Szymon Nowak

Inconsistent behavior of vs

Reported by Szymon Nowak | March 24th, 2011 @ 07:42 PM

I'm not sure how exactly should behave, as it's not really documented (at least not here:, but it's behavior is inconsistent:

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, :through => :friendships

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User"
user.friendships # => []
user.friendships # => [#<Friendship id: nil, user_id: 1, friend_id: nil>]

# but
user.friends # => []
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

    Szymon Nowak March 24th, 2011 @ 07:51 PM

    Turns out that HasManyThroughAssociation has a simple alias: alias_method :new, :build, which HasManyAssociation 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=""></a>

People watching this ticket