cách sử dụng ransackable_scopes trong ROR

Bùi Văn Thượng
Đã trả lời thg 6 23, 7:47 SA
Theo ý kiến cá nhân của mình, thì có thể sửa lại code cho gọn như sau:
# GET /products
def index
@products = Product.with_active_subscription(params[:is_active_subscription])
end
# model
scope :with_active_subscription, -> (flag) {
flag = ActiveModel::Type::Boolean.new.cast(flag)
return if flag.blank?
where(has_active_subscription: flag)
}
0
cách sử dụng ransackable_scopes trong ROR

Bùi Văn Thượng
Đã trả lời thg 6 23, 7:39 SA
class << self
def ransackable_scopes(auth_object = nil)
[:with_active_subscription] # <-- dùng symbol
end
end
0
Lỗi `undefined method `find_script_name' for nil:NilClass` trong Rails

Bùi Văn Thượng
Đã trả lời thg 5 4, 2023 8:21 SA
bạn có thể sử dụng Safe Navigation Operator (&) nhé.
0
Các kĩ năng
Tổ chức
Chưa có tổ chức nào.