[英]Getting the list of followers with there attributes
我有两节课
class User < ActiveRecord::Base
has_many :followers
has_many :contacts, :through => :followers
end
和
class Follower < ActiveRecord::Base
belongs_to :user
belongs_to :contact, :class_name => "user", :foreign_key => "contact_id"
end
我想从用户那里获取所有关注者属性(名称,姓氏,地址等)。
当我尝试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">]>
当我执行User.first.contacts
它将失败,并显示以下错误NameError: uninitialized constant User::user
我该怎么做才能获得User.all
这样的关注者属性?
NameError:未初始化的常量User :: user
在follower
模型中,对于所属belongs_to :contact
:class_name
选项,您使用了错误的值 。 应该是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.