Ruby on rails 多对多用户和组,但组有所有者
我很难理解这件事。我正试图建立一种关系,这种关系允许:Ruby on rails 多对多用户和组,但组有所有者,ruby-on-rails,many-to-many,Ruby On Rails,Many To Many,我很难理解这件事。我正试图建立一种关系,这种关系允许: 用户可以属于多个组 组可以有多个用户 组的所有者是用户 集团所有权可以转让 我已经建立了多对多关系,但我似乎不知道如何设置所有权功能 到目前为止,我的模型中有以下内容: class Group < ActiveRecord::Base has_and_belongs_to_many :users attr_accessible :name, :description, :isPublic, :tag
- 用户可以属于多个组
- 组可以有多个用户
- 组的所有者是用户
- 集团所有权可以转让
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
类组
任何帮助都将不胜感激 我认为一个解决方案是定义一个新的
归属关系,从集团到所有者。因此,您需要在groups
表中添加一个新的user\u 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.groups
,拥有他所属的所有组,也可以拥有他所拥有的所有组user.owned\u groups
。
对于组,您可以有其所有者:group.owner
及其所有成员:group.users
如果要更改所有者,只需使用new\u user
执行group.owner=new\u user
即可开始一个user
实例
请注意,我很快选择了关联和外键命名,您当然可以自定义它。您可以通过以下几种方式进行设置:
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
类组
链接表方法如何?
因为,如果您有一个链接表,将用户和组作为用户和组,那么您实际上可以在用户和组相互属于的时刻将另一个新属性存储到该表中
比如当用户John属于男孩组时,他很淘气。当用户John属于该组时,他很聪明。所以在users\u groups表中,我可以存储如下内容
使用者
团体
id | Name
1| boys
2| doctors
3| beggars
用户组
user_id | group_id | info
1 | 1 | "naughty"
1 | 2 | "wearing white coat"
1 | 3 | "broke"
2 | 1 | "liar"
这不是很有趣吗?。但是,是的,我们必须处理好我们之间的关系。但只是一个想法
刚才提到了使用链接表的优点。实施
这是肖恩·希尔在他的回答中提到的
在您的用户模型中,添加一个布尔字段作为所有者,以便在创建用户时,您可以指定他是否为所有者。
因此,如果你想对照组进行检查,你可以按以下方法进行
group.users.each do|u|
u、 所有者?
//逻辑就在这里
结束亲爱的肖恩,我刚刚写了一个优点(作为答案),如果我们有一个连接模型,即链接表,我们可以做什么。这篇文章的前半部分是最好的答案。如果您确实希望一个组只有一个所有者,请对成员资格模型进行验证。这种方法还允许您拥有多个所有者(或管理员),而无需更改架构。谢谢。就我个人而言,我会使用第一种方法,但我只会给出选择。使用第一种方法时,我出现了一个错误“找不到表成员身份”。我将如何编写db迁移以获得成员资格?我可以使用rails生成模型成员资格,但我不确定应该使用哪些参数。您可以使用rails g模型成员资格组:参考用户:参考
user_id | group_id | info
1 | 1 | "naughty"
1 | 2 | "wearing white coat"
1 | 3 | "broke"
2 | 1 | "liar"