Edward OG wrote:
Sorry to resuscitate this post, but is it possible to do something with
a has_and_belongs_to_many relationship?
e.g. in this case, I want to be able to have the methods
Researcher.projects, Project.principal_investigator, and
Project.co_investigators
class Researcher < ActiveRecord::Base
has_and_belongs_to_many :projects
end
class Projects < ActiveRecord::Base
has_one :principal_investigator, :class_name => “Researcher”,
:foreign_key => “princ_inv” # A Project can only have one principal
investigator
has_many :co_investigator, :class_name => “Researcher”, :foreign_key
=> “co_inv” # Projects can have many co-investigators
end
Looks to be an interesting problem you are tackling. Being a PI, I’d
love to hear more about your application.
Meanwhile…
In this particular case, I think you have your models set up
incorrectly.
In theory, PIs, co-investigators, and researchers are all people (or
users) who happen to have different roles. The role will be different
on different projects, perhaps you need something like…
class User < ActiveRecord::Base
has_many :collaborations
end
class Collaboration < ActiveRecord::Base
belongs_to :user
belongs_to :project
has_one :role
end
class Project < ActiveRecord::Base
has_many :collaborations
end
you could do this with a HABTM table with attributes, but this seems a
little more natural to me, and I can see the collaboration model
becoming more complex.