author avatar

syedsibtain

Fri Aug 02 2024

Ransack adds several class methods to ActiveRecord::Base for managing search and sort capabilities. These methods allow us to define which attributes, associations, and scopes are accessible through Ransack queries, thus enhancing security and control.

ransackable_attributes: Defines which model attributes can be used in Ransack searches.
For example, only the order_number, status, and business_unit fields can be used in Ransack searches.


def self.ransackable_attributes(auth_object = nil)
    %w[order_number status business_unit]
  end


ransackable_associations: Specifies which model associations are accessible for searching.
For examle, only the customer and vendor associations are allowed for searching.


def self.ransackable_associations(auth_object = nil)
    %w[customer vendor]
  end


ransortable_attributes: Lists which attributes can be used for sorting results.
In this example, sorting is allowed only by order_number and created_at


def self.ransortable_attributes(auth_object = nil)
    %w[order_number created_at]
  end


ransackable_scopes: Determines which custom scopes can be applied in searches.

#rails #ransack #search