1
我很難理解如何在涉及連接表時如何將關於俱樂部的所有者別名附加到用戶。我試圖建立一個應用程序,用戶可以創建一個俱樂部並將其他用戶添加到該俱樂部。活躍記錄擁有和屬於許多別名
用戶有很多俱樂部作爲所有者和屬於許多俱樂部作爲成員。
A俱樂部有很多用戶是成員和屬於 a用戶是它的主人。
我希望能夠得到以下信息:
user.clubs => Returns all clubs the User is a member of
user.clubs_as_owner => Returns all the clubs they own
club.owner => Returns the User Jane Doe
club.members => Returns the Users: Jane D., Ashley K., Marry P.
。
class User < ActiveRecord::Base
has_many :user_clubs
has_many :clubs, through: :user_clubs, :class_name => "Club",
:foreign_key => "member_id"
has_many :clubs_as_owner, through: :user_clubs, :class_name => "Club",
:foreign_key => "owner_id"
end
class Clubs < ActiveRecord::Base
has_many :user_clubs
has_many :members, through: :user_clubs, :class_name => "User",
:foreign_key => "user_id"
has_one :owner, through: :user_clubs, :class_name => "User",
:foreign_key => "user_id"
end
class UserClubs < ActiveRecord::Base
belongs_to :user
belongs_to :club
end