Kevin Behan
Kevin Behan

Reputation: 496

Ruby on Rails skip update callback on specific method in model

In my Student model I trigger a method after_update, but the problem is I have to save within the method. This results in an infinite recursive loop:

class Student < ActiveRecord::Base

after_update :delete_inactive_student_schedules

def delete_inactive_student_schedules
  if self.status_was == active and self.status == paused
    self.missing_schedule_at = nil
    self.save!
    self.schedules.destroy_all
  end
end

Is there a way to skip the update callback after the save within the method?

Upvotes: 1

Views: 821

Answers (1)

Sander Garretsen
Sander Garretsen

Reputation: 1723

You can use update_columns to update colums without triggering callbacks or validations.

So in your case

if self.status_was == active and self.status == paused
  self.update_columns(:missing_schedule_at => nil)
  self.schedules.destroy_all
end

http://apidock.com/rails/ActiveRecord/Persistence/update_columns

Upvotes: 1

Related Questions