Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a different table record on an aasm event #804

Open
flyingboy007 opened this issue Jan 10, 2023 · 0 comments
Open

create a different table record on an aasm event #804

flyingboy007 opened this issue Jan 10, 2023 · 0 comments

Comments

@flyingboy007
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant