0
cách sử dụng ransackable_scopes trong ROR
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.
Add a comment