简体   繁体   English

获取具有那里属性的关注者列表

[英]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. 当我尝试User.first.followers我得到了很好的列表。

=> #<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 当我执行User.first.contacts它将失败,并显示以下错误NameError: uninitialized constant User::user

How could I do to get there followers attributes like a User.all do ? 我该怎么做才能获得User.all这样的关注者属性?

NameError: uninitialized constant User::user NameError:未初始化的常量User :: user

You have a wrong value for :class_name option for the belongs_to :contact in the follower model. follower模型中,对于所属belongs_to :contact :class_name选项,您使用了错误的值 It should be User not user 应该是User而不是user

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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