app/models/contact.rb in e9_crm-0.1.6 vs app/models/contact.rb in e9_crm-0.1.7
- old
+ new
@@ -11,10 +11,12 @@
##
# Associations
#
belongs_to :company
+ has_many :owned_deals, :class_name => 'Deal'
+ has_and_belongs_to_many :associated_deals, :class_name => 'Deal'
has_many :users, :inverse_of => :contact, :dependent => :nullify do
##
# Resets the primary user on a contact
@@ -155,9 +157,23 @@
tagged_with(tags, :show_hidden => true, :any => true)
else
where("1=0")
end
}
+
+ def self.available_to_deal(deal)
+ return all unless deal.persisted?
+
+ sql = <<-SQL
+ SELECT distinct contacts.* FROM `contacts`
+ LEFT OUTER JOIN `contacts_deals`
+ ON `contacts_deals`.`contact_id` = `contacts`.`id`
+ WHERE (`contacts_deals`.`deal_id` IS NULL
+ OR `contacts_deals`.`deal_id` != #{deal.id})
+ SQL
+
+ find_by_sql(sql)
+ end
#
# Carrierwave
#
mount_uploader :avatar, AvatarUploader