#20 new
Gaius Centus Novus

State Transitions Return true but do Nothing

Reported by Gaius Centus Novus | June 22nd, 2009 @ 10:40 AM

I have a class:

class Item < ActiveRecord::Base
  include AASM
  aasm_initial_state :available
  aasm_state :available
  aasm_state :in_use
  aasm_event(:check_out) do
    transitions :from => :available, :to => :in_use
  end
  aasm_event(:check_in) do
    transitions :from => :in_use, :to => :available
  end
end

This works:

i = Item.create!
i.write_attribute(:state, 'in_use')
# => "in_use"
i.save!
# => true
i.state
# => "in_use"

But this doesn't do anything:

i = Item.create!
i.check_out!
# => true
i.state
# => "available"

I'm on AASM 2.0.5 and Rails 2.3.2. I tried applying the patch for a (seemingly) related bug at http://www.pastie.org/444480 but that did nothing.

Comments and changes to this ticket

  • Gaius Centus Novus

    Gaius Centus Novus June 22nd, 2009 @ 11:54 AM

    An oddity: after a state transition, read_attribute returns the correct value even though state doesn't:

    i = Item.create!
    i.check_out!
    i.state
    # => 'available'
    
    i.read_attribute(i.class.aasm_column)
    # => 'in_use'
    
  • Gaius Centus Novus

    Gaius Centus Novus June 22nd, 2009 @ 11:59 AM

    The problem was that I was using alias_attribute instead of aasm_column:

    # wrong
    class Item
      include AASM
      alias_attribute :aasm_state, :state
    end
    
    # right
    class Item
      include AASM
      self.aasm_column :state
    end
    

    AASM wrote to ":aasm_state," which I assumed would write through, but didn't.

    This ticket can be closed.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Library for adding state machines to Ruby classes. Includes persistence layers for things like ActiveRecord. Formerly known as acts_as_state_machine.

People watching this ticket

Pages