Solved

Many to Many relations between 3 tables

Posted on 2008-09-30
5
1,279 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rails 3 Equivalent 7 353
issues with Nginx + Passenger Production setup - Loading time/request time delay 4 959
return captures in array 5 259
remove rows where duplicate 10 121
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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