在 Rails 中获取由 AASM Gem 生成的作用域的补集

Obtain the Complement of a Scope Generated by AASM Gem in Rails

这个问题实际上是由 AASM gem 开发人员回答的。

我在我的 rails 应用程序模型 (Mongoid) 中使用 AASM gem。我知道您会自动为每个定义的状态生成范围。例如,有这个...

class Order
  include Mongoid::Document
  include Mongoid::Timestamps
  include AASM

  field :aasm_state

  aasm do
    state :pending, :initial => true
    state :received

    event :receive do
      transitions :from => :pending, :to => :received
    end
  end
end

...将允许我做:Order.pendingOrder.received.

我的问题很简单,你是否也生成范围来获得给定状态的补码?。类似于:Order.not_pending 以获得状态不同于 'pending'[= 的 Orders 34=]?.

我知道构建自己的示波器非常容易,就像这样...

scope :not_pending, ->{ ne(aasm_state: "pending") }

...但我想确保您没有定义它,因为如果您定义了它,我宁愿使用您的范围也不愿使用我自己的范围。

不,AASM 不提供相反的范围。我们考虑过这一点,但最终决定不这样做,以免给范围 space 增加不必要的负担。