# Acts\_as\_state\_machine override :initial state

**URL:** <https://www.ruby-forum.com/t/acts-as-state-machine-override-initial-state/125273>\
**Category:** Rails\
**Created:** [January 7, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/acts-as-state-machine-override-initial-state/125273 "2008-01-07T18:46:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hirodusk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6f9a4e/32.png) [@hirodusk](https://www.ruby-forum.com/u/hirodusk)\
**Post date:** [January 7, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/acts-as-state-machine-override-initial-state/125273/1 "2008-01-07T18:46:29Z")

</div>

Hello,

I’m not sure if this is where to post this, but i cannot find much  
information/support for acts\_as\_state\_machine.

I have a Friendship model which implements acts\_as\_state\_machine. I  
have an :initial state of :pending, but i would like to create a  
Friendship model and save it directly with :accepted status.

I have tried Friendship.create(:state =\> “accepted”) and Friendship.new,  
friendship.state = “accepted”, .save!..but neither have worked. The  
only way i can accomplish this is by doing:  
friendship = Friendship.create  
friendship.state = “accepted”  
friendship.save!

However, i do not like this since it has an unnecessary DB hit. Anybody  
know how i can do this?

thanks,  
Adam

---

<div class="post-metadata">

**Author:** ![hirodusk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6f9a4e/32.png) [@hirodusk](https://www.ruby-forum.com/u/hirodusk)\
**Post date:** [January 7, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/acts-as-state-machine-override-initial-state/125273/2 "2008-01-07T19:44:08Z")

</div>

You could modify set\_initial\_state in acts\_as\_state\_machine.rb as  
follows:

```
    def set_initial_state #:nodoc:
      write_attribute self.class.state_column,

```

self.class.initial\_state.to\_s if self.state.nil?  
end

That way if you specify an initial state it won’t assign the default  
initial state in before\_create. You’d probably also need to modify  
run\_initial\_state\_actions (which is run by the after\_create callback)  
along the lines of:

```
    def run_initial_state_actions
      if self.state.to_sym === self.class.initial_state.to_sym
        initial = self.class.read_inheritable_attribute(:states)

```

[self.class.initial\_state.to\_sym]  
initial.entering(self)  
initial.entered(self)  
end  
end

So that the actions for the default initial state don’t run.

This does rather defeat the purpose of using acts\_as\_state\_machine  
though. 🙂

On Jan 7, 5:46 pm, Adam W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hirodusk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6f9a4e/32.png) [@hirodusk](https://www.ruby-forum.com/u/hirodusk)\
**Post date:** [January 7, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/acts-as-state-machine-override-initial-state/125273/3 "2008-01-07T20:50:31Z")

</div>

On Jan 7, 9:46 am, Adam W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a Friendship model which implements acts\_as\_state\_machine. I  
> have an :initial state of :pending, but i would like to create a  
> Friendship model and save it directly with :accepted status.

The thing is, then you’ve no longer got a state machine. It’s like  
having a goto in code. Could the :initial state route it to :accepted?  
As far as the db access goes, that’s generally the last thing you  
should worry about, in my opinion.

///ark
