Skip to content
This repository has been archived by the owner on Jul 25, 2019. It is now read-only.

Commit

Permalink
Destroy geocode_grade_walkzone_schools when schools, geocodes, grade_…
Browse files Browse the repository at this point in the history
…levels and grade_level_schools are destroyed
  • Loading branch information
joelmahoney committed Oct 30, 2012
1 parent 0901a28 commit 22ab6a8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/models/geocode.rb
@@ -1,5 +1,6 @@
class Geocode < ActiveRecord::Base
has_many :address_ranges
has_many :geocode_grade_walkzone_schools
has_many :geocode_grade_walkzone_schools, :dependent => :destroy
belongs_to :assignment_zone

end
3 changes: 2 additions & 1 deletion app/models/grade_level.rb
@@ -1,6 +1,7 @@
class GradeLevel < ActiveRecord::Base
has_many :grade_level_schools
has_many :grade_level_schools, :dependent => :destroy
has_many :schools, :through => :grade_level_schools
has_many :geocode_grade_walkzone_schools, :dependent => :destroy

def assignment_zone_schools(geocoded_address, assignment_zone)
schools = self.schools.where(:assignment_zone_id => assignment_zone)
Expand Down
7 changes: 7 additions & 0 deletions app/models/grade_level_school.rb
@@ -1,6 +1,7 @@
class GradeLevelSchool < ActiveRecord::Base
belongs_to :school
belongs_to :grade_level
before_destroy :delete_geocode_grade_walkzone_schools

def total_choices
if first_choice.present?
Expand Down Expand Up @@ -54,4 +55,10 @@ def math_percentile
scores = GradeLevelSchool.find_all_by_grade_number(self.grade_number).map(&:math_score).compact.sort
((scores.index(self.math_score) / scores.size.to_f) * 100).to_i
end

private

def delete_geocode_grade_walkzone_schools
GeocodeGradeWalkzoneSchool.where(grade_level_id: self.grade_level.try(:id), school_id: self.school.try(:id)).delete_all
end
end
2 changes: 1 addition & 1 deletion app/models/school.rb
Expand Up @@ -11,7 +11,7 @@ class School < ActiveRecord::Base
belongs_to :parcel
belongs_to :principal
belongs_to :state
has_many :geocode_grade_walkzone_schools
has_many :geocode_grade_walkzone_schools, :dependent => :destroy

attr_accessor :eligibility, :eligibility_index
# before_save :recalculate_school_assignment so if they change a school location or add a school it will be reindexed TODO
Expand Down

0 comments on commit 22ab6a8

Please sign in to comment.