你可以將它設置了幾個方面:
1)使用一個連接模型,並放置一個標誌上的連接模型,指定該組成員是所有者。
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
attr_accessible :name, :description, :isPublic, :tag_list, :owner
end
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
#this table has a flag called owner and thus a method called owner?
end
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, through: :memberships
attr_accessible :name, :description, :owner_id
end
2)保留您現有的HABTM並添加另一個連接模型來跟蹤所有權。
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :group_ownerships
has_many :owners, through: :group_owernships, class_name: "User"
attr_accessible :name, :description, :isPublic, :tag_list, :owner
end
class GroupOwnership < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
has_many :group_ownerships
has_many :owned_groups, through: :group_owernships, class_name: "Group"
attr_accessible :name, :description, :owner_id
end
親愛的肖恩,我只寫了什麼,我們可以做的優點(作爲一個答案),如果我們有一個連接模型,即一個鏈接表。 – beck03076 2013-03-15 21:51:59
這是前半部分的最佳答案。如果您真的只想爲一個組擁有一個所有者,請對Membership模型進行驗證。這種方法還可讓您擁有多個所有者(或管理員),而無需更改架構。 – 2013-03-16 01:02:51
謝謝。就我個人而言,我會做第一種方法,但我全部都是在給予選擇。 – 2013-03-16 01:03:49