Skip to content

Commit 6f8932a

Browse files
committed
setting group and user 多對多資料庫關聯
1 parent ab81566 commit 6f8932a

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

app/models/group.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ class Group < ActiveRecord::Base
44
has_many :posts
55
belongs_to :owner, class_name: "User", foreign_key: :user_id
66

7+
has_many :group_users
8+
has_many :memebers, through: :group_users, source: :user
9+
710
def editable_by?(user)
811
user && user == owner
912
end

app/models/group_user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
class GroupUser < ActiveRecord::Base
2+
belongs_to :user
3+
belongs_to :group
24
end

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ class User < ActiveRecord::Base
66

77
has_many :groups
88
has_many :posts
9+
10+
has_many :group_users
11+
has_many :participated_groups, through: :group_users, source: :group
12+
913
end

0 commit comments

Comments
 (0)