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

ransack_alias doesn't work in associations #1520

Open
matgaw opened this issue Aug 29, 2024 · 1 comment
Open

ransack_alias doesn't work in associations #1520

matgaw opened this issue Aug 29, 2024 · 1 comment

Comments

@matgaw
Copy link

matgaw commented Aug 29, 2024

class Parent
  ransack_alias :full_name, :first_name_or_last_name
end

class Child
  belongs_to :parent
end

Working scenario:

Parent.ransack(full_name_cont: 'a').result # works correctly

Error scenario:
Child.ransack(parent_full_name_cont: 'a').result
Result:

undefined method `type' for nil:NilClass (NoMethodError)

      attr.klass.columns.find { |column| column.name == name }.type
                                                              ^^^^^
@dougjq
Copy link

dougjq commented Feb 15, 2025

This particular line of code is problematic for ActiveAdmin users, as well. If you use ActiveAdmin #includes on a has_one through, you will trigger a call to ransack. ActiveAdmin is feeding ransack the association through the parent, with the result that ransack is told to expect to find grandparent_id that does not exist. This line throws undefined method `type' for nil:NilClass.

Suppose class Whatever < ApplicationRecord has_one :grandparent, through: :parent. ActiveAdmin sends ransack instructions to look for whatevers.grandparent_id.

I can't help but feel there is inadequate error checking here in ransack. Further up the trace in

lib/ransack/nodes/condition.rb:25:in `extract'

there is this comment:

        # TODO: Figure out what to do with multiple types of attributes,
        # if anything. Tempted to go with "garbage in, garbage out" here.

Not being a ransack expert I can't say what the solution would be. But I can say this is a new error introduced since rack 3 and it seems improperly handled.

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

No branches or pull requests

2 participants