Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Many to Many relations between 3 tables

Posted on 2008-09-30
5
Medium Priority
?
1,285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 14

Accepted Solution

by:
wesgarrison earned 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

722 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