Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created April 28, 2014 11:14
Show Gist options
  • Save mikecmpbll/11368699 to your computer and use it in GitHub Desktop.
Save mikecmpbll/11368699 to your computer and use it in GitHub Desktop.
class Contact < ActiveRecord::Base
has_and_belongs_to_many :clients, class_name: "School"
after_save :update_primary
def name
title + " " + forename + " " + surname
end
private
def update_primary
if primary
self.clients.first.contacts.where.not(id: self.id).update_all(primary: false)
end
end
end
SQL (0.8ms) INSERT INTO `schools` (`created_at`, `dfes_code`, `key_code`, `local_authority_id`, `name`, `private`, `status`, `updated_at`, `url`) VALUES ('2014-04-28 11:12:46', '0875', 'aaa5', '14', 'aaaaaa', '616161350000', '3. No data', '2014-04-28 11:12:46', 'aaa.cpoms.net')
(0.3ms) INSERT INTO `apps_schools` (`school_id`, `app_id`) VALUES (254, 1)
SQL (0.3ms) INSERT INTO `contacts` (`created_at`, `email`, `forename`, `notes`, `primary`, `role`, `surname`, `telephone_alternative`, `telephone_primary`, `title`, `updated_at`) VALUES ('2014-04-28 11:12:46', '', 'a', '', 1, '', 'a', '', '', 'a', '2014-04-28 11:12:46')
School Load (18.0ms) SELECT `schools`.* FROM `schools` INNER JOIN `contacts_schools` ON `schools`.`id` = `contacts_schools`.`school_id` WHERE `contacts_schools`.`contact_id` = 10 ORDER BY `schools`.`id` ASC LIMIT 1
(16.0ms) ROLLBACK
Completed 500 Internal Server Error in 99ms
NoMethodError (undefined method `contacts' for nil:NilClass):
app/models/contact.rb:13:in `update_primary'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment