2013-03-15 99 views
6

我在尝试理解/包裹我的大脑时遇到了困难。我试图创建关系,使这个:多对多用户和群组,但群组拥有所有者

  • 用户可以属于多个组
  • 组可以有很多用户
  • 一组具有所有者是用户
  • 组所有权可以转让

我已经建立了多对多的关系,但我似乎无法理解如何设置所有权功能。

这里是我迄今为止在我的模型:

class Group < ActiveRecord::Base 
     has_and_belongs_to_many :users 
     attr_accessible :name, :description, :isPublic, :tag_list, :owner 
    end 

    class User < ActiveRecord::Base 
     has_and_belongs_to_many :groups 
     attr_accessible :name, :description, :owner_id 
    end 

任何帮助将不胜感激!

回答

11

你可以将它设置了几个方面:

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 
+1

亲爱的肖恩,我只写了什么,我们可以做的优点(作为一个答案),如果我们有一个连接模型,即一个链接表。 – beck03076 2013-03-15 21:51:59

+1

这是前半部分的最佳答案。如果您真的只想为一个组拥有一个所有者,请对Membership模型进行验证。这种方法还可让您拥有多个所有者(或管理员),而无需更改架构。 – 2013-03-16 01:02:51

+0

谢谢。就我个人而言,我会做第一种方法,但我全部都是在给予选择。 – 2013-03-16 01:03:49

3

我认为,一种解决方案可能是定义一个新的belongs_to关系从组到所有者。因此,您需要在groups表中添加新的user_id列。

class Group < ActiveRecord::Base 
    has_and_belongs_to_many :users 
    belongs_to :owner, class_name: 'User', foreign_key: 'user_id' 
    attr_accessible :name, :description, :isPublic, :tag_list, :owner 
end 

class User < ActiveRecord::Base 
    has_and_belongs_to_many :groups 
    has_many :owned_groups, class_name: 'Group', foreign_key: 'user_id' 
    attr_accessible :name, :description, :owner_id 
end 

我想这就是你需要的。与用户一起,您可以拥有他所属的所有组,并且您可以拥有他拥有的所有组user.owned_groups。 对于一个组,您可以让它的所有者:group.owner及其所有成员:group.users

如果要更改所有者只是做group.owner = new_usernew_user开始User实例

注意,我赶紧拿起任命为联想和外键,当然你可以自定义。

0

怎么样的链接表的方法呢?

因为,如果你有一个用户和组的链接表作为users_groups,那么你实际上可以在用户和组彼此属于彼此的时刻将另一个新属性存储到该表中。

就像是,约翰用户属于男孩组,他很顽皮。当用户属于工程师约翰时,他很聪明。所以在users_groups表中,我可以存储如下所示的内容。

用户

id | Name 
1 | John 
2 | Steve 

id | Name 
1| boys 
2| doctors 
3| beggars 

Users_Groups

user_id | group_id | info 
1  | 1  | "naughty" 
1  | 2  | "wearing white coat" 
1  | 3  | "broke" 
2  | 1  | "liar" 

这不是好玩的?但是,是的,我们必须对Has进行研究并且属于许多关系。但只是一个想法!

只是提到了具有链表的优点。实施 ,由肖恩希尔在他的答案照顾。

0

在你的用户模型中添加一个boolean字段作为所有者,所以在创建用户的时候你可以分配他是否是所有者。
所以,如果你想检查组,你可以做到如下。

group.users.each do | u |
u.owner?
//逻辑到这里