Lets say I want to create a database of finished coding projects, developers, and their roles.
The models look like this:
project has_and_belongs_to_many developers
developers has_and_belongs_to_many projects
The developers_projects table look like this:
int: developer_id, project_id
string: role
Lets day I have 3 Developers and 2 projects with their respective id and names as
1, Ashley
2, Bob
3, Charles
1, Tic Tac Toe
2, Snake
And their actual roles being
Project 1:
- Manager: Ashley
- Coder: Bob, Charles
Project 2:
- Manager: Charles
- Coder: Ashley
Is it possible to define a managers and coders object under projects?
like:
class Project < ActiveRecord::Base
has_and_belongs_to_many :developers
has_and_belongs_to_many :managers, :class_name => "developers", :condition => "role = 'manager'"
has_and_belongs_to_many :coders, :class_name => "developers", :condition => "role = 'coder'"
end
(this does not work)
Thank you in advance,
has_and_belongs_to_manyallows to create join tables without declaring model. As far as you want to have attribute in this table you should create separate model, for example, ProjectRoles in your case: