1

我有两个在我的模型中不起作用的回调。他们也不会引发任何错误消息。

第一个回调是:after_update :state_closed当我从“已解决”或“已取消”视图中选择工单状态时,我想用它来关闭工单。所以,我希望它在这种情况下被关闭

第二个回调是after_create :assign_state,我想用它来表示已分配票证或未分配票证,因此如果employee_id 为空白,则表示票证尚未分配给任何员工。如果employee_id 不是黑色,则分配此票

这是我的票.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
  end

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

这是来自服务器日志,当我选择“已解决”时,例如,如果我的回调正在工作,它会更新为“已解决”,那么在这种情况下,它应该从已解决更改为已关闭,但这并没有发生

Started PUT "/tickets/1" for 127.0.0.1 at 2013-09-14 21:46:54 +0200
Processing by TicketsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"LZRTSjq9EWqgG6ub3xpd7fioWNtY1SSzy5XQA8ZNep0=", "ticket"=>{"ticket_state"=>"solved"}, "id"=>"1"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  State Load (0.1ms)  SELECT "states".* FROM "states" 
  Ticket Load (0.1ms)  SELECT "tickets".* FROM "tickets" WHERE "tickets"."id" = ? LIMIT 1  [["id", "1"]]
   (0.0ms)  begin transaction
   (0.2ms)  UPDATE "tickets" SET "ticket_state" = 'solved', "updated_at" = '2013-09-14 19:46:54.926307' WHERE "tickets"."id" = 1
   (95.2ms)  commit transaction
Redirected to http://localhost:3000/tickets
Completed 302 Found in 100ms (ActiveRecord: 96.0ms)
4

1 回答 1

1

每个问题如下:

state_closed由于大小写问题而未被调用:“已解决”不等于“已解决”。更改大写以匹配,或在字符串都小写时比较字符串。

assign_state可能会被调用,但不会持续存在,因为一旦模型更改,您就不会真正保存模型。在创建更新后尝试保存。

于 2013-09-14T20:25:13.597 回答