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

validate_uniqueness_of in polymorphic scope with AASM gem in Ruby 3.0+ fails #1516

Open
rasmar opened this issue Oct 24, 2022 · 1 comment
Open

Comments

@rasmar
Copy link

rasmar commented Oct 24, 2022

In one of our projects we've faced issue with the gem, while using validate_uniqueness_of in scope of polymorphic attribute with associated model using AASM. Shoulda engine creates TestModel (via Mutex) that is symlinked to the root one. This doesn't work for models using AASM gem - the state machine definition is missing in cache. This works in Ruby 2.7+ but not 3.0+.

Possible reason: https://bugs.ruby-lang.org/issues/16792
Failing place in AASM: https://github.com/aasm/aasm/blob/master/lib/aasm/persistence/active_record_persistence.rb#L151

Screenshot 2022-10-24 at 16 24 44

class OffersSetWrappersAssociation < ApplicationRecord
  belongs_to :displayable, polymorphic: true
  validates :offers_set_wrapper_id, uniqueness: { scope: %i[displayable_type displayable_id] }
end

class Offer < ApplicationRecord
  include AASM

  has_many :offers_set_wrappers_associations,
           as: :displayable,
           inverse_of: :displayable,
           dependent: :destroy

  aasm column: :status, enum: true, requires_lock: true, whiny_persistence: true do
    state :draft, initial: true
    state :active

    event :activate do
      transitions from: %i[draft inactive], to: :active
    end
  end
end

FactoryBot.define do
  factory :offers_set_wrappers_association do
    offers_set_wrapper
    association :displayable, factory: :offer
  end
end

I'm not sure whether this is AASM gem bug or yours, but I found only the Mutex feature that might cause this behaviour and you are the one using it in your code.

@night91
Copy link

night91 commented Jul 25, 2023

This is happening to me too. Rails 7.0, Ruby 3.2.2.

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

2 participants