Skip to content

Instantly share code, notes, and snippets.

@spacecowb0y
Created January 18, 2013 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacecowb0y/4562253 to your computer and use it in GitHub Desktop.
Save spacecowb0y/4562253 to your computer and use it in GitHub Desktop.
God bless AR!
class Order < ActiveRecord::Base
## Setup accessible (or protected) attributes for your model
attr_accessible :project_id, :developer_id, :due_date, :status, :notes, :is_delivered, :deliveted_at, :completed_at
## Virtual attributes
attr_accessor :pages
## Associations
belongs_to :project
belongs_to :developer, :inverse_of => :assigned_orders,
:class_name => "User"
## Callbacks
before_save :assign_developer, :if => :developer_id_changed?
## Custom methods
def assign_developer
# we create an assignment for the new developer on the project
# if it doesn't have one already
self.project.assignments.create!(:user_id => self.developer_id) if self.project.assignments.find_by_user_id(self.developer_id).nil?
# if the old developer doesn't have any other order assigned on this project
# we should (?) delete the assignment to it
self.project.assignments.find_by_user_id(self.developer_id_was).destroy unless self.project.orders.find_by_developer_id(self.developer_id_was).nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment