Rails:在 where 子句中使用相同模型的列值在模型中添加范围

Rails: Adding scope in a model using column values of same model in where clause

我是 rails 的新手。我正在尝试使用 where 子句创建一个范围。在那个 where 子句中,我想使用同一模型的列值。但我不知道这样做的正确方法。请帮帮我。这是代码片段:

attr_accessible :booked_qty,
                :cancelled_qty,
                :quantity_shipped

这是我要创建的范围:

scope :shipped_qty_less_than_max_tolerance_qty, where("quantity_shipped < ?",self.booked_qty-self.cancelled_qty)

那么编写这个作用域的最佳方式是什么?提前致谢!

范围是正确的,如果您需要了解更多关于范围的信息,请务必查看 Rails Guides v3.2

在访问了所有可能的链接并尝试了不同的可能性后,这对我有用:

scope :shipped_qty_less_than_max_tolerance_qty, where("quantity_shipped < booked_qty-cancelled_qty")

感谢大家support.Cheers!