Gem WhereableScope: 把 scope 用作 where 參數

mizuhashi · 2026年01月29日 · 最后由 zhandao 回复于 2026年01月29日 · 40 次阅读

https://github.com/onyxblade/whereable_scope

例子:

class Order < ActiveRecord::Base
  has_one :order_address
  has_one :address, through: :order_address

  scope :by_address, ->(address) { joins(:order_address).where(order_addresses: { address: address }) }

  # 把by_address scope註冊為可用的where參數
  whereable_scope :by_address, as: :address
end

# 現在你可以:
Order.where(address: address)

僅供娛樂

没有看太懂,为什么不:

Order.joins(:address).merge(address)
需要 登录 后方可回复, 如果你还没有账号请 注册新账号