{"action":"created","issue":{"url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/16","labels_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/16/comments","events_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/16/events","html_url":"https://github.com/state-machines/state_machines-activerecord/issues/16","id":70716437,"number":16,"title":"use_transactions: false not respected in execution","user":{"login":"rosskevin","id":136564,"avatar_url":"https://avatars.githubusercontent.com/u/136564?v=3","gravatar_id":"","url":"https://api.github.com/users/rosskevin","html_url":"https://github.com/rosskevin","followers_url":"https://api.github.com/users/rosskevin/followers","following_url":"https://api.github.com/users/rosskevin/following{/other_user}","gists_url":"https://api.github.com/users/rosskevin/gists{/gist_id}","starred_url":"https://api.github.com/users/rosskevin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rosskevin/subscriptions","organizations_url":"https://api.github.com/users/rosskevin/orgs","repos_url":"https://api.github.com/users/rosskevin/repos","events_url":"https://api.github.com/users/rosskevin/events{/privacy}","received_events_url":"https://api.github.com/users/rosskevin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-04-24T15:17:45Z","updated_at":"2015-04-24T20:07:56Z","closed_at":null,"body":"I should note that I am debugging this in a `before_transition`.\r\n\r\nI'm not sure what is going on yet, but I can see that the [machine.rb#within_transaction](https://github.com/state-machines/state_machines/blob/master/lib/state_machines/machine.rb#L1862) is not run (which seems to evaluate this flag).   With the active record integration, it runs the transaction every time.  I'm not sure if it is overwriting that check in machine or what is occuring, but I cannot seem to turn off transactions as [this code](https://github.com/state-machines/state_machines-activerecord/blob/master/lib/state_machines/integrations/active_record.rb#L536) is always run:\r\n\r\n```ruby\r\n      # Runs a new database transaction, rolling back any changes by raising\r\n      # an ActiveRecord::Rollback exception if the yielded block fails\r\n      # (i.e. returns false).\r\n      def transaction(object)\r\n        result = nil\r\n        object.class.transaction do\r\n          raise ::ActiveRecord::Rollback unless result = yield\r\n        end\r\n        result\r\n      end\r\n```\r\n\r\nThe state_machine is configured with:\r\n\r\n```ruby\r\n    state_machine :state, initial: :uninitialized, use_transactions: false do\r\n```"},"comment":{"url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/comments/96049149","html_url":"https://github.com/state-machines/state_machines-activerecord/issues/16#issuecomment-96049149","issue_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/16","id":96049149,"user":{"login":"rosskevin","id":136564,"avatar_url":"https://avatars.githubusercontent.com/u/136564?v=3","gravatar_id":"","url":"https://api.github.com/users/rosskevin","html_url":"https://github.com/rosskevin","followers_url":"https://api.github.com/users/rosskevin/followers","following_url":"https://api.github.com/users/rosskevin/following{/other_user}","gists_url":"https://api.github.com/users/rosskevin/gists{/gist_id}","starred_url":"https://api.github.com/users/rosskevin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rosskevin/subscriptions","organizations_url":"https://api.github.com/users/rosskevin/orgs","repos_url":"https://api.github.com/users/rosskevin/repos","events_url":"https://api.github.com/users/rosskevin/events{/privacy}","received_events_url":"https://api.github.com/users/rosskevin/received_events","type":"User","site_admin":false},"created_at":"2015-04-24T20:07:56Z","updated_at":"2015-04-24T20:07:56Z","body":"So who is responsible for interpreting the transaction option?  StateMachine or TransitionCollection?  \r\n\r\nTransitionCollection appears to get into the interpreting of transactions and even has an [errant hack](https://github.com/state-machines/state_machines/blob/master/lib/state_machines/transition_collection.rb#L181) that picks the first state_machine and uses transactions (problem 1), but only if [**it** is configured with `transaction: true`](https://github.com/state-machines/state_machines/blob/master/lib/state_machines/transition_collection.rb#L36) for which in my execution it is [hardcoded with `transaction: false`](https://github.com/state-machines/state_machines/blob/master/lib/state_machines/transition_collection.rb#L195).  On top of this, the [active_record#around_save](https://github.com/state-machines/state_machines-activerecord/blob/master/lib/state_machines/integrations/active_record.rb#L510) forces transactions with no regard for either the TransitionCollection's `transactions` option or the state_machine's `use_transactions` option.\r\n\r\nSo here is what I see (and a proposal):\r\n1.  StateMachine is coded properly, and should be the interpreter of `use_transactions`.  If TransitionCollection needs access to the option for interpretation, it should come from (in order of priority) 1. transition's `:transaction` option, or 2. state_machine's `use_transaction`, or 3. default to hardcoded.\r\n2.  TransitionCollection is [improperly making decisions](https://github.com/state-machines/state_machines/blob/master/lib/state_machines/transition_collection.rb#L181) about transactions and it should be removed (delegated to state_machine#within_transaction)\r\n3. AttributeTransitionCollection (subclass) is [hardcoded with `transaction: false`](https://github.com/state-machines/state_machines/blob/master/lib/state_machines/transition_collection.rb#L195) and should be given the same treatment as TransitionCollection specified above.\r\n4. [active_record#around_save](https://github.com/state-machines/state_machines-activerecord/blob/master/lib/state_machines/integrations/active_record.rb#L510) should not interpret transactions, this is the job of `StateMachine#within_transaction`\r\n5. active_record.rb should only need to implement `#transaction` for the underlying ORM, which [it already does](https://github.com/state-machines/state_machines-activerecord/blob/master/lib/state_machines/integrations/active_record.rb#L536)\r\n\r\nThis would be 2 pull requests because it will cross-project boundaries, but I think it's the correct/intended set of responsibilities.\r\n\r\n@seuros - your thoughts?\r\n"}}