Solved

Many to Many relations between 3 tables

Posted on 2008-09-30
5
1,274 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If you need to access extra information on the junction table, use :through.
If not, use HABTM for simplicity.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now