Skip to content

Instantly share code, notes, and snippets.

@wprater
Created Apr 23, 2010
Embed
What would you like to do?
class Project < ActiveRecord::Base
# has_one :client, :class_name => "Client", :foreign_key => "client_id"
has_one :salesperson, :class_name => "Contact", :foreign_key => "sales_contact_id"
has_many :managers_projects
has_many :managers, :through => :managers_projects,
:select => 'managers.*, managers_projects.project_id, managers_projects.manager_id, managers_projects.`primary`'
# We want all the extra fields on the consultants_projects association
has_many :consultants_projects
has_many :consultants,
:through => :consultants_projects,
:select => 'consultants.*, consultants.id,'+
'consultants_projects.id as consultants_projects_id, ' +
'consultants_projects.project_id,' +
'consultants_projects.pay_rate,' +
'consultants_projects.pay_rate,' +
'consultants_projects.bill_rate,' +
'consultants_projects.start_date,' +
'consultants_projects.end_date,' +
'consultants_projects.enabled AS consultants_projects_enabled'
def primary_manager
return managers.first.id if managers.length == 1
managers.each do |manager|
return manager.id if manager['primary'] == 't'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment