Gem WhereableScope: 把 scope 用作 where 參數

mizuhashi · January 29, 2026 · Last by zhandao replied at January 29, 2026 · 40 hits

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)
You need to Sign in before reply, if you don't have an account, please Sign up first.