簡體   English   中英

獲取具有那里屬性的關注者列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM