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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Walters
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> 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
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Jan 7, 9:46 am, Adam Walters <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maybe Matching Threads
- having a problem with acts_as_state_machine
- acts_as_state_machine doesn't error on invalid state transition?
- random exception driving me crazy: SecurityError (Insecure: can't modify array)
- acts_as_state_machine: SecurityError calling insecure method
- ActiveRecord::AssociationTypeMismatch (User(#54754560) expected, got User(#54510280))