你可以将它设置了几个方面:
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