Topic: need advice on creating tables/associations between Users and Projects

Hi everyone, I just joined this forum, so I am sorry if this question has been asked before. I did do a search and found a few interesting topics, but I wasn't able to find the exact answer I was looking for.

I am trying to create a simple Project Management tool with multiple users. Each one of my users can work on multiple projects and each project can have multiple users.

I have a User model and a Project model. I am not sure if I am supposed to use HABTM or the has_many/through method. I am also not sure which one of my models is supposed to have the habtm code and which one the belongs_to code.

If someone could give me some sample code or point me to any existing articles, i would greatly appreciate it.

Thank you.

Re: need advice on creating tables/associations between Users and Projects

i8ramin wrote:

I have a User model and a Project model. I am not sure if I am supposed to use HABTM or the has_many/through method. I am also not sure which one of my models is supposed to have the habtm code and which one the belongs_to code.

It mainly comes down to this: do you want to store extra information in the join? You may want to store when the project was assigned to the user for example, or if the user has completed his part in the project, or a description of the users role in the project, etc. If you do then you would want to use has_many :through, otherwise you may want to use HABTM.

If you are going with has_many :through then you need to come up with a name for the join. You may want to call it "assignment" or "task". For example:

class User < ActiveRecord::Base
  has_many :assignments
  has_many :projects, :through => :assignments
end

class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :project
end

class Project < ActiveRecord::Base
  has_many :assignments
  has_many :users, :through => :assignments
end


If you are going with HABTM then you wouldn't have this Assignment model, you would just create a "projects_users" table with only two foreign key columns (project_id and user_id). You would then set it up like this:

class User < ActiveRecord::Base
  has_and_belongs_to_many :projects
end

class Project < ActiveRecord::Base
  has_and_belongs_to_many :users
end

Railscasts - Free Ruby on Rails Screencasts

Re: need advice on creating tables/associations between Users and Projects

Interesting questions and answers so far.  I'm looking at a very very similar data model, and the hiccup I'm running into is making the association between two existing objects.

My simplified use case looks like :

http://homepage.mac.com/eableson/xfer/simple_use_case.jpg

I see a lot of examples designed for adding new items and building the requisite relationship as part of the operation, but nothing yet for linking existing objects.  I could directly manipulate the join table (as I would have done with php or asp), but I'm sure rails provides a better abstraction method...

Anyone have some ideas on the rhtml and controller bits required to accomplish this?