0

I am using the Ransack gem in my Rails application. It is working as expected, but how can I reorder the list of attributes in the dropdown box for the attribute select? Unfortunately for me, "Name" of the User is near the bottom of the list, when ideally it would be at the top.

.field
  = f.attribute_fields do |a|
    = a.attribute_select associations: [:terms, :contact_informations, :educational_experiences, :home_towns, :local_residences, :permanent_addresses, :emergency_contact_informations]

The above is my current attribute_select, right now it lists all of the Users attributes, then attributes for each of the associations. In my models, I am using the following to filter out some of the attributes.

def self.ransackable_attributes(auth_object = nil)
    super - ['id', 'first_name', 'middle_name', 'last_name', 'created_at', 'updated_at', 'encrypted_password', 'reset_password_token', 'reset_password_sent_at', 'remember_created_at',
              'sign_in_count', 'current_sign_in_at', 'last_sign_in_at', 'current_sign_in_ip', 'last_sign_in_ip']
  end

1 Answer 1

0

I found this on the ransack github repository:

def ransackable_attributes(auth_object = nil)
  column_names + _ransackers.keys
end

So in my model, I can override this method by just doing something different.

  def self.ransackable_attributes(auth_object = nil)
    column_names.unshift(column_names.delete('name', 'role')) - ['id', 'first_name', 'middle_name', 'last_name', 'created_at', 'updated_at', 'encrypted_password', 'reset_password_token', 'reset_password_sent_at', 'remember_created_at',
                  'sign_in_count', 'current_sign_in_at', 'last_sign_in_at', 'current_sign_in_ip', 'last_sign_in_ip']+ _ransackers.keys
  end

Since column_names is just an array, I can remove an element from the array, then add it with unshift. If someone has a better solution, please still post it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.