简体   繁体   中英

Getting the list of followers with there attributes

I have two classes

class User < ActiveRecord::Base
  has_many :followers
  has_many :contacts, :through => :followers
end

and

class Follower < ActiveRecord::Base
  belongs_to :user
  belongs_to :contact, :class_name => "user", :foreign_key => "contact_id" 
end

I want to get all the followers attributes (name, surname, address etc.) from a User.

when I try User.first.followers I got the good list.

=> #<ActiveRecord::Associations::CollectionProxy [#<Follower id: 1, user_id: 1
, contact_id: 2, created_at: "2016-05-02 14:01:35", updated_at: "2016-05-02 14:0
1:35">]>

When I do User.first.contacts it fails with this error NameError: uninitialized constant User::user

How could I do to get there followers attributes like a User.all do ?

NameError: uninitialized constant User::user

You have a wrong value for :class_name option for the belongs_to :contact in the follower model. It should be User not user

class Follower < ActiveRecord::Base
  belongs_to :user
  belongs_to :contact, :class_name => "User", :foreign_key => "contact_id" 
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM