Steve
Steve

Reputation: 11

Rails groups / user members associations best practices


I'm pretty new to Ruby and Rails and I'm developing a web app that requires multiple groups, each of which has many members, but the groups and members are completely separate from one another.

i.e.: group1 has members 1, 2 and 3
group2 has members 4, 5 and 6
group3 has members 7, 8 and 9

also, each of the members can submit posts to their group's directory.

I'm trying to figure out the best logic to approach this with in terms of database management, etc. Is it as simple as creating a groups model, which has_many members, which has_many posts and going from there?

I guess I'm just worried that things will eventually start to get jumbled up with the group login, and the individual user logins, etc.

Thanks for any help with this, I realize it is quite a broad question at this point.

Upvotes: 1

Views: 637

Answers (1)

Luke
Luke

Reputation: 4925

I would model it as follows:

class Group < ActiveRecord::Base
  has_many :members
  has_many :posts, :through => :members
end

class Member < ActiveRecord::Base
  belongs_to :group
  has_many   :posts
end

class Post < ActiveRecord::Base
  belongs_to :member
end

Upvotes: 3

Related Questions