Take the 2-minute tour ×

I have two models; team and project. The application I am trying to create lets the team to create a new project. Sometimes, a team can do a joint project with another team.

So what is the correct association that I should use between them? Right now, I have


has_many :projects


belongs_to :team

I am not sure if "has_and_belongs_to_many" association will do since RoR guide uses two model plus a weak model

share|improve this question
add comment

1 Answer

You can use the has_and_belongs_to_many association with two models if you also create a join table like so:

class AddTeamsProjectsJoinTable < ActiveRecord::Migration
  def self.up
    create_table :teams_projects, :id => false do |t|
    t.integer :teams_id
    t.integer :projects_id

  def self.down
    drop_table :teams_projects

Then in your models:


has_and_belongs_to_many :projects


has_and_belongs_to_many :teams

Then you can access all the projects for one team using @team.projects, or all the teams for one project with @project.teams

share|improve this answer
add comment

Your Answer


