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