views:

396

answers:

3

I have a Service Types table containing id and name of a couple of dozen services.

I have a Projects table that has to have a list of Proposed Services, and a list of Accepted Services.

I know that I would use HABTM on both sides with a project_service_types table in between.

I can't quite figure out what to do when I have 2 different relationships between the same table. I suspect it uses the :join_table and :associated_forign_key, but I can't get it to work in my app.

thanks.

+3  A: 

For that you'd probably want to use has_many :through as in:

class ProposedService < ActiveRecord::Base
    belongs_to :project
    belongs_to :service_type

class AcceptedService < ActiveRecord::Base
    belongs_to :project
    belongs_to :service_type

class Projects < ActiveRecord::Base
    has_many :proposed_services
    has_many :accepted_services
    has_many :service_types, :through => :proposed_services
    has_many :service_types, :through => :accepted_services

class ServiceTypes < ActiveRecord::Base
    has_many :proposed_services
    has_many :accepted_services
    has_many :projects, :through => :proposed_services
    has_many :projects, :through => :accepted_services

The many-to-many section here:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

explains this in more detail. Hope this helps!

Adam Alexander
+1  A: 

I solved it using HABTM...

class ServiceType < ActiveRecord::Base
  has_and_belongs_to_many :accepted_projects, :class_name => "Project", :join_table => :projects_accepted_types
  has_and_belongs_to_many :proposed_projects, :class_name => "Project", :join_table => :projects_proposed_types
end

class Project < ActiveRecord::Base
  has_and_belongs_to_many :accepted_types, :class_name => "ServiceType", :join_table => :projects_accepted_types
  has_and_belongs_to_many :proposed_types, :class_name => "ServiceType", :join_table => :projects_proposed_types
end
anithri
That looks like it will do the trick; the only thing to be aware of is if you may later want to have data located on the association itself (such as a notes field in the ProjectsProposedTypes model) you would want the has_many :through option detailed in my answer. Good luck with the project!
Adam Alexander
+2  A: 

While you can solve this with habtm, what you're talking about is the use-case for has_many :through. You want to attach a bit of information along with the relationship. To do this you create a join model that represents the relationship.

In the end this allows you to treat your service proposal as a first-class "thing" in your domain. When the service is accepted you can just change the status. This also saves a join.

Migration

create_table :project_services do |t|
  t.references :project
  t.references :service_type
  t.string :status
end

Models

class ProjectService < ActiveRecord::Base
  belongs_to :project
  belongs_to :service
end

class Project < ActiveRecord::Base
  has_many :project_services
  has_many :accepted_services, :through => :project_services,
    :conditions => { :status => 'accepted' }
  has_many :proposed_services, :through => :proposed_services,
    :conditions => { :status => 'proposed' }
end

class Service < ActiveRecord::Base
  has_many :project_services
  has_many :accepted_projects, :through => :project_services,
    :conditions => { :status => 'accepted' }
  has_many :proposed_projects, :through => :proposed_services,
    :conditions => { :status => 'proposed' }
end
Sam C