Permalink
Browse files

Fix populate sites after pool changed

  • Loading branch information...
1 parent 8d208df commit a2d58c579ebe270075f8517c0a87aacd63d02e3b @legiar committed Jan 31, 2012
Showing with 23 additions and 2 deletions.
  1. +23 −2 app/models/slice.rb
View
25 app/models/slice.rb
@@ -1,10 +1,31 @@
class Slice < ActiveRecord::Base
+
belongs_to :host
belongs_to :last_host, :class_name => 'Host'
belongs_to :network_address
belongs_to :pool
- has_many :site_instances
- has_many :sites, :through => :site_instances
+
+ has_many :site_instances, :dependent => :destroy
+ has_many :sites, :through => :site_instances, :dependent => :destroy
validates :name, :presence => true, :uniqueness => true
+
+ before_save :store_old_values
+ after_save :populate_instances
+
+ private
+
+ def store_old_values
+ @previously_changed = changes
+ end
+
+ def populate_instances
+ if previous_changes.has_key?("pool_id")
+ sites.clear
+ pool.sites.each do |site|
+ site_instances.create(:site => site)
+ end
+ end
+ end
+
end

0 comments on commit a2d58c5

Please sign in to comment.