Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Many to Many relations between 3 tables

Posted on 2008-09-30
5
Medium Priority
?
1,286 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
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 a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month7 days, 17 hours left to enroll

824 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