Hi everyone.  I've made a default change to has_many :through
associations that I felt was important to make before they're
released.  This is after some tickets and confusion I've seen
regarding has_many :through.

class Connection < ActiveRecord::Base
  belongs_to :user
  belongs_to :channel
end

class Channel < ActiveRecord::Base
  has_many :connections
  has_many :contacts, :through => :connections, :class_name => 'User'
end

What's misleading in this case, is has_many :through isn't using
:class_name to specify what model to load, it's using it to use the
:user association in Connection as the source reflection.  has_many
:through is a unique association that actually uses the source
reflection to get the class_name and foreign_key it needs for the
query.  Instead, change that model to:

class Channel < ActiveRecord::Base
  has_many :connections
  has_many :contacts, :through => :connections, :source => :user
end

I apologize for the hassle, but you do get a descriptive and helpful
error message:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not
find the source association(s) contact or contacts in model
Connection.  Try 'has_many :contacts, :through => :connections,
:source => <name>'.  Is it one of :channel or :user?

--
Rick Olson
http://techno-weenie.net
_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to