Search code examples
ruby-on-rails-4activerecordspreeransack

Ransack search and sorting not working with some attributes


Ransack search work fine with some attributes:

Spree::Callback.all.ransack(name_cont: 1).result

SELECT "spree_callbacks".* FROM "spree_callbacks" WHERE ("spree_callbacks"."name" ILIKE '%1%')

And don't work with others...

Spree::Callback.all.ransack(phone_cont: 1).result

SELECT "spree_callbacks".* FROM "spree_callbacks"

What's wrong?

P.S. Callback is my custom model under Spree module

And example of Callback object:

Spree::Callback.first
  Spree::Callback Load (1.4ms)  SELECT  "spree_callbacks".* FROM "spree_callbacks"  ORDER BY "spree_callbacks"."id" ASC LIMIT 1
=> #<Spree::Callback:0x00000009eb68c0 id: 39, 
phone: "123", 
comment: "", 
created_at: Wed, 12 Jul 2017 03:37:03 UTC +00:00, 
updated_at: Wed, 12 Jul 2017 03:41:34 UTC +00:00, 
name: "123", 
processed: true>

Solution

  • @nuT707 - To be able to search by using ransack gem, you will first have to whitelist all those fields on which you wanna perform search. So in you callback.rb model add the following line -

    self.whitelisted_ransackable_attributes |= %w[phone]

    and now try to search with ransack.

    Hope this helps you.