ruby on rails - Could not find the source association(s) :followed in model Relationship. -
i following michael hartl's rails book , getting following error message:
activerecord::hasmanythroughsourceassociationnotfounderror: not find source association(s) :follower in model relationship. try 'has_many :following, :through => :active_relationships, :source => <name>'. 1 of ?
when calling michael.follower
(where michael
user object).
here associations:
class user < activerecord::base has_many :active_relationships, class_name: "relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, :through => :active_relationships, source: :follower has_many :followers, :through => :passive_relationships, source: :followed end class relationship < activerecord::base belongs_to :follower, class_name: "user", foreign_key: "follower_id" belongs_to :followed, class_name: "user", foregin_key: "followed_id" validates :follower_id, presence: true validates :followed_id, presence: true end
if understand correctly, have many-to-many relation between user , relationship models through relationship-model (i'm sorry tautology).
i suggest change
has_many :following, :through => :active_relationships, source: :follower has_many :followers, :through => :passive_relationships, source: :followed
to
has_many :following, :through => :active_relationships, class_name: "user" has_many :followers, :through => :passive_relationships, class_name: "user"
Comments
Post a Comment