Skip to content

Instantly share code, notes, and snippets.

@coop
Created June 30, 2011 11:29
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 coop/1056044 to your computer and use it in GitHub Desktop.
Save coop/1056044 to your computer and use it in GitHub Desktop.
class LineItemObserver < ActiveRecord::Observer
def before_save record
current_date = record.order.start
reservations = record.product.reservations
while current_date < record.order.end
reservation = reservations.where(:day => current_date).first
if reservation.nil?
reservations.create :quantity => record.quantity, :day => current_date
else
reservation.update_attributes :quantity => reservation.quantity + record.quantity
end
current_date += 1.day
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment