2013-03-15 73 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?
//邏輯到這裏