Asked thg 6 10, 7:29 SA 48 0 0
  • 48 0 0
0

cách sử dụng ransackable_scopes trong ROR

Share
  • 48 0 0

model code:

     scope :with_active_subscription, -> (flag = nil) {
        pp "Value received: #{flag.inspect}"
        return all if flag.blank? || flag.to_s == ''

        if flag.to_s == 'true'
          # Logic cho true case
          where(has_active_subscription: true)
        elsif flag.to_s == 'false'
          # Logic cho false case  
          where(has_active_subscription: false)
        else
          all
        end
      }

      class << self
        def ransackable_scopes(auth_object = nil)
          %w[with_active_subscription]
        end
      end

controller code:

# GET /products
  def index
    @q = Product.ransack(search_params)
    @products = @q.result(distinct: true)
  end

  private

    def search_params
    params.fetch(:q, {}).permit(
      :with_active_subscription,
    )
  end

=> hiện không nhận được giá trị của flag trong "scope :with_active_subscription", mọi người giúp mình với.

Viblo
Let's register a Viblo Account to get more interesting posts.