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

[Incorrect behavior / Issue] Attributes validation #1532

Open
dmytro-strukov opened this issue Oct 9, 2024 · 2 comments
Open

[Incorrect behavior / Issue] Attributes validation #1532

dmytro-strukov opened this issue Oct 9, 2024 · 2 comments

Comments

@dmytro-strukov
Copy link

dmytro-strukov commented Oct 9, 2024

Ransack version: >= 4.0
Rails: >= 7.x

Possible scenario when the issue occurs:

class Article < ApplicationRecord
  def self.ransackable_attributes(auth_object = nil)
    [ :title ]
  end
end

What is the problem:

  • When attribute validation does not pass the end user does not know anything about it
  • Overall in Ruby in such type of cases developers use symbols because they by default have a similar behavior as constants and do not take additional memory

The method: valid?

Options to resolve:

  • Raise error or log warn message when the attribute didn't pass the validation
  • Instead of looking in an array that will take O(n) time rather use a Hash or Set that will take O(1). Rails provide also Hash With Indifferent Access that will make it possible to use symbols and strings as keys

@scarroll32 What do you think? I can implement it and cover via test cases

@scottbarrow
Copy link

scottbarrow commented Oct 11, 2024

I believe Ransack has an option to raise on unknown attributes https://activerecord-hackery.github.io/ransack/going-further/other-notes/#handling-unknown-predicates-or-attributes
i.e. .ransack!(...) will raise on unknown attributes if the option is true
but errors are not added to the base object since this isn't necessarily user error

@dmytro-strukov
Copy link
Author

I believe Ransack has an option to raise on unknown attributes https://activerecord-hackery.github.io/ransack/going-further/other-notes/#handling-unknown-predicates-or-attributes i.e. .ransack!(...) will raise on unknown attributes if the option is true but errors are not added to the base object since this isn't necessarily user error

Thank you, I am wondering why it is not enabled by default 🤔

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