多对多用户和组,但组有所有者 [英] Many-to-many Users and groups, but groups have owners

查看:107
本文介绍了多对多用户和组,但组有所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试理解/解决这个问题时遇到了麻烦.我正在尝试创建一个允许这种关系的关系:

I'm having trouble trying to understand/wrap my brain around this. I'm trying to create a relationship that allows this:

  • 用户可以属于多个组
  • 组可以有很多用户
  • 一个网上论坛的所有者是用户
  • 组的所有权可以转让

我已经建立了多对多关系,但是我似乎不明白如何设置所有权功能.

I've got the many-to-many relationship set up, but I can't seem to understand how to set up the ownership functionality.

这是我到目前为止在模型中所拥有的:

here is what i have so far in my models:

    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

任何帮助将不胜感激!

推荐答案

您可以通过以下几种方法进行设置:

You can set it up a couple of ways:

1)使用联接模型,并在联接模型上放置一个标志,以指定组成员是所有者.

1) Use a join model and place a flag on the join model that specifies that the group member is an owner.

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并添加另一个联接模型以跟踪所有权.

2) Keep your existing HABTM and add another join model for tracking ownership.

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

这篇关于多对多用户和组,但组有所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆