回调会使堆栈级别过高

时间:2013-09-14 11:31:16

标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.2 callback

我需要为:assign_state和:state_closed运行回调但是它们不起作用并且提升

Stack level to deep

Ticket.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

2 个答案:

答案 0 :(得分:2)

您的assign_state在无限递归中调用自身:它调用#save,然后触发#assign_state回调。

答案 1 :(得分:0)

如果您未在回拨中拨打submit或使用$('#someform').trigger('submit'); ,则可以使用$('#someform').submit(); 回调。

before_update

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