Mostafa Hussein
Mostafa Hussein

Reputation: 11940

Callback is not working

I have two callbacks that aren't working in my model. They don't raises any error messages either.

The first callback is: after_update :state_closed and I want to use this to close the ticket when I select ticket state from the view 'solved' or 'canceled'. So, I want it in this case is to be closed

The second callback is after_create :assign_state and I want to use this to say the ticket is assigned or its not assigned so if the employee_id is blank thats mean the ticket is not assigned to any employee yet. If employee_id is not black so this ticket is assigned

Here is my 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
  ...

This is from server logs when i choose "solved" for example it is updated to "solved" if my callbacks are working then in this case it should change from solved to closed but that's not happening

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)

Upvotes: 1

Views: 2832

Answers (1)

Tyler
Tyler

Reputation: 11509

The issues with each are as follows:

state_closed is not being called because of capitalization issue: "solved" does not equal "Solved". Change the capitalizations to match, or compare the strings when they're both downcased.

assign_state is probably being called, but not persisting because you never actually save the model once it's changed. Try saving after you update on create.

Upvotes: 1

Related Questions