Sitelet https://github.com/aasm/aasm/issues/804
Skip to content

create a different table record on an aasm event #804

Description

@flyingboy007

I have a simple aasm model like the one below. When calling post.fill!(data), it needs to create a new record on a different table with the data value. This works in the below code by calling generate_new_log_record on before_enter.

class Post < ApplicationRecord
 belongs_to :parent_model
 include AASM
  aasm column: :status do
    state :posted, initial: true
    state :filled, before_enter: Proc.new { |data| generate_new_log_record(data) }
   
    event :fill do
      transitions from: [:posted], to: :filled
    end
    
  end

  def generate_new_log_record(data)
# how to make this inside the event transaction????
    parent_model.log_records.create(start_date: expected_start_time, end_date: expected_end_time, data: data)
  end

end

The issue is that even when for some reason generate_new_log_record is failed to create a new record, the state is still changing to filled. How can we make sure it happens inside a single DB transaction so that when the generate_new_log_record fails it undo every change that happened?

Thank you

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions