module ErpBaseErpSvcs::Extensions::ActiveRecord::HasContacts::SingletonMethods

Public Instance Methods

find_by_email(email, contact_purpose=nil) click to toggle source
# File lib/erp_base_erp_svcs/extensions/active_record/has_contacts.rb, line 24
def find_by_email(email, contact_purpose=nil)
  if contact_purpose
    self.joins(:contacts => [:contact_purposes])
        .joins("INNER JOIN email_addresses on email_addresses.id = contacts.contact_mechanism_id
      and contacts.contact_mechanism_type = 'EmailAddress'")
        .where('contact_mechanism_type = ?', 'EmailAddress')
        .where('contact_purposes.internal_identifier = ?', contact_purpose)
        .where('email_address = ?', email).readonly(false).first
  else
    self.joins(:contacts)
        .joins("INNER JOIN email_addresses on email_addresses.id = contacts.contact_mechanism_id
      and contacts.contact_mechanism_type = 'EmailAddress'")
        .where('contact_mechanism_type = ?', 'EmailAddress')
        .where('email_address = ?', email).readonly(false).first
  end
end