Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several models have invalid "name" included in ransackable_attributes #3179

Closed
mdesantis opened this issue Apr 15, 2019 · 0 comments · Fixed by #3180
Closed

Several models have invalid "name" included in ransackable_attributes #3179

mdesantis opened this issue Apr 15, 2019 · 0 comments · Fixed by #3180

Comments

@mdesantis
Copy link
Contributor

I stumbled upon a Ransack misconfiguration:

self.default_ransackable_attributes = %w[id name]

name is included by default in all the models, even the ones which don't have a name column, such as Spree::Variant, causing Ransack searches like this:

Spree::Variant.ransack('name_cont' => 'asd', 's' => 'id desc').result.to_sql

fail with the following error:

> bundle exec rails c
Loading development environment (Rails 5.2.3)
[1] pry(main)> Spree::Variant.ransack('name_cont' => 'asd', 's' => 'id desc').result.to_sql
NoMethodError: undefined method `type' for nil:NilClass
from gems/ransack-2.1.1/lib/ransack/adapters/active_record/context.rb:28:in `type_for'

Instead of discarding the name matcher.

Solidus Version: 2.9.0.alpha@4d53bdc92

To Reproduce

cd sandbox
bundle exec rails r "puts Spree::Variant.ransack('name_cont' => 'asd', 's' => 'id desc').result.to_sql"

Current behavior

undefined method `type' for nil:NilClass

Expected behavior

SELECT "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL ORDER BY "spree_variants"."id" DESC
mdesantis added a commit to mdesantis/solidus that referenced this issue Apr 15, 2019
Fixes solidusio#3179. `name` column included by default within default
ransackable attributes causes ransack searches with search matchers
based on `name` column to crash when appied on models which don’t have a
`name` column. This PR removes the `name` column from default
ransackable attributes and adds it explicitly on models which support
search by name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant