I am trying to find all variants in spree where is_master
is false
but ransack is not working as expected. The reason I am using ransack is so I can run this search over the spree api so I can make a request like /api/v1/variants?q[is_master_true]='0'
to get all the non master variants.
In the documentation
>> User.ransack(awesome_false: '1').result.to_sql
=> SELECT "users".* FROM "users" WHERE ("users"."awesome" = 'f')
In practice
> Spree::Variant.ransack(is_master_false: '1').result.to_sql
=> "SELECT \"spree_variants\".* FROM \"spree_variants\" WHERE \"spree_variants\".\"deleted_at\" IS NULL"
The is_master_false
is ignored in the sql
Another search like sku_eq
works fine
> Spree::Variant.ransack(sku_eq: '17636').result.to_sql
=> "SELECT \"spree_variants\".* FROM \"spree_variants\" WHERE \"spree_variants\".\"deleted_at\" IS NULL AND \"spree_variants\".\"sku\" = '17636'"
Why does is_master_false: '1'
do nothing to the sql query created instead of finding records where is_master = false
?
@Qwertie: For ransack search you need to first whitelist all the attributes on which you want to perform ransack search.
Currently, in the Variant model only weight sku are whitelisted.
So you need to whitelist is_master field yourself
For doing just create a decorator for variant in spree/models with name variant_decorator.rb and write
Spree::Variant.class_eval do
### WHITELISTED ATTRIBUTES ###
self.whitelisted_ransackable_attributes |= ['is_master']
end
Or in spree.rb
add Spree::Variant.whitelisted_ransackable_attributes.push('is_master')
Now restart your server or rails console and try it out.