Forum: Ruby on Rails ActiveRecord - has_many :through w auto referenced table

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-01-03 17:22
Happy New Year to all rubyist and railist ...  only 3 days in new yaer
and already a question ....

I am using a 'User' table which is self-referenced via a joint
Credential table..
according to the follwoing declarations :

class User < ActiveRecord::Base
  has_many :credentials, :dependent => true
  has_many  :referees, :through => :credentials
  has_many  :referenced_users, :through => :credentials

class Credential < ActiveRecord::Base
  belongs_to  :referee, :class_name => 'User', :foreign_key =>
  belongs_to  :referenced_user, :class_name => 'User' ,  :foreign_key =>

In my controller, when I try to get all referees from a user :

@contacts = current_user. referees   # = 22

I get an error :

Mysql::Error: Unknown column 'credentials.user_id' in 'where clause':
SELECT users.* FROM users INNER JOIN credentials ON =
WHERE (credentials.user_id = 22)

what's missing in the User declarations ?

thanks for your lights !

This topic is locked and can not be replied to.