1

我需要为 :assign_state 和 :state_closed 运行回调,但它们不起作用并引发

Stack level to deep

票务.rb

class Ticket < ActiveRecord::Base
    before_save :default_values
  after_update :state_closed
  after_create :assign_state

  attr_accessible :description, :title, :employee_department_id, :user_id, :first_name,
   :last_name , :email, :state_id, :employee_id, :ticket_state, :assign_state


  belongs_to :employee_department
  belongs_to :user
  belongs_to :state
  belongs_to :employee

  has_many :replies

  def default_values
    self.state_id = 3 if self.state_id.nil?
  end

  def to_label
    ticket_state.to_s
  end

  def state_closed
    if self.ticket_state == "Solved" || self.ticket_state == "Canceled" 
      self.ticket_state = "Closed"
      self.save
    end
  end

def assign_state
  if self.employee_id.nil?
    self.assign_state = "Un-assigned"
  else 
    self.assign_state = "Assigned"
  end 
  self.save
end

   Ticket.all.each do |ticket|
    if ticket.ticket_state.blank?
      ticket.ticket_state = 'open'
    end
    ticket.save
  end
end
4

2 回答 2

2

您的 assign_state 以无限递归方式调用自身:它调用#save,进而触发#assign_state回调。

于 2013-09-14T11:33:44.470 回答
0

before_update如果您不调用save或在回调中使用,则可以使用update_attributes回调。

before_update :state_closed

def state_closed
  if self.ticket_state == "Solved" || self.ticket_state == "Canceled" 
    self.ticket_state = "Closed"
  end
end

这将更新 ticket_state 并且原始更新调用将更新它。所以这种方式更新只被调用一次。

于 2017-04-26T11:16:12.453 回答