Solved

Many to Many relations between 3 tables

Posted on 2008-09-30
5
1,278 Views
Last Modified: 2013-11-13
Hi all,

I have four tables.

Projects,
Roles,
Users
Projects_Roles_Users

Now I want to establish Many to Many relationship between Projects, Roles, Users.

I have search on the net for many to many relation and found lot of examples specifying relationship on two table using has_and_belongs_to or has many through options. How can i achieve this task of specifying many to many relationship between three tables using has_and_belongs_to and has_many through options.

Thanks,
0
Comment
Question by:vadhwana_divyesh
  • 3
5 Comments
 
LVL 14

Accepted Solution

by:
wesgarrison earned 500 total points
ID: 22616966
Just to clarify: a user can belong to many projects and can have a different role (or multiple roles) on each of those projects?

Certainly, this could be simpler if: each user only had one role per project, or each user was always the same role for every project. That said, maybe your application isn't like that.

I wouldn't call your join model "Projects_Roles_Users".  Instead, call it something like "Participation" or "Permission" or something like that.  I'll use Permission, because it makes sense to me that a Project's permissions would include the users and what roles they have.  
project.rb
has_many :permissions
 
permission.rb
belongs_to :project
belongs_to :user
has_and_belongs_to_many :roles
 
user.rb
has_many :permissions
 
role.rb
has_and_belongs_to_many :permissions

Open in new window

0
 
LVL 1

Expert Comment

by:ozataman
ID: 22632355
Hi,

Here is how I would do it:

Let's call your Projects_Users_Roles something a little bit more intuitive, such as RoleMapping

class RoleMapping < ActiveRecords::Base
  belongs_to :project
  belongs_to :user
  belongs_to :role
end

Then each other class would use has_many :through relationships.

For example, the role class would have:

class Role < ActiveRecord::Base
  has_many :project_user_roles
  has_many :projects, :through => :project_user_roles
  has_many :users, :through => :project_user_roles
end

Cheers,
OA
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22666330
If you have a moment, please let me know what I could have provided to get an A on this question so that I can improve in the future.

Thanks!
0
 

Author Comment

by:vadhwana_divyesh
ID: 22667099
Thanks for your reply, You solution worked for me. You have used has_and_belongs_to relationship in your example but i was looking for resolution in both the ways using has_and_belongs_to and has_many through. I am still not clear when to use has_and_belongs_to and when to use has_many through.
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22667203
If you need to access extra information on the junction table, use :through.
If not, use HABTM for simplicity.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question