我有两个回调在我的模型中无效。它们也不会引发任何错误消息。
第一个回调是:after_update :state_closed
当我从“已解决”或“已取消”视图中选择票证状态时,我想使用它来关闭票证。所以,我想在这种情况下关闭
第二个回调是after_create :assign_state
,我想使用它来说明故障单已分配或未分配,因此如果employee_id为空,则表示该故障单尚未分配给任何员工。如果employee_id不是黑色,则分配此票证
这是我的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
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)
答案 0 :(得分:1)
每个问题如下:
由于资本化问题, state_closed
未被调用:“已解决”不等于“已解决”。更改大写以匹配,或者比较字符串在它们都是低级时。
assign_state
可能正在被调用,但不会持续存在,因为一旦模型发生变化,你就永远不会保存模型。在create。更新后尝试保存。