I have the following After Filters:
User Model:
def check_for_custom_district
unless self.custom_district.blank?
district = District.new(:name => custom_district, :state_id => state_id, :school_type_id => school_type_id)
if district.save(false)
school = School.new(:name => custom_school, :state_id => state_id, :country_id => 1, :source => "User")
if school.save(false)
district.schools << school
update_attribute(:school_id, school.id)
end
end
end
end
def check_for_custom_school
if self.custom_district.blank? and self.custom_school.present?
school = School.new(:name => custom_school, :state_id => state_id, :country_id => 1, :source => "User", :school_district_id => district_id)
school.save(false)
update_attribute(:school_id, school.id)
end
end
I ran some debugging to output the results to the console and the code is hitting the check_for_custom_district
method and causing an infinite loop for some reason. Not sure how to stop this... any ideas?
I'm assuming it's an
after_save
filter.So lets say you just saved your
model
.It triggers the
after_save
filter. But, in your filter, you actually callupdate_attribute(:school_id, school.id)
, which also saves the current model, which triggers yourafter_save
filter again.That's where your infinite loop is coming from.
One way to get around this might be not to use the
after_save
filter at all. For example, just re-implement thesave
method: