-
Notifications
You must be signed in to change notification settings - Fork 39
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
How to force to use LIKE query on nonstring fields? #15
Comments
Hi, that's currently not supported out of the box. Moreover, to support it while staying database agnostic we need to add type cast statements to the generated sql, because e.g. postgres doesn't support it without explicit type casts. It would be possible to add it as a new feature via something like search_scope ... do
options :numbers, :cast => :string
end The obvious downside of this would be: it can no longer be treated as an integer, such that Order.search "number > 10" will work on strings instead of integers and imo produces unexpected results. Moreover, there are performance issues, but if you're using wildcard LIKE queries (instead of fulltext indices), this should be negligible. I'll consider this feature to be added in the future, but can't give an ETA, but you're welcome to add a PR. So, currently you can add an additional string column to your schema or change your search query to: Order.search "number >= 20590001 and number <= 20590009 |
Great! Thanks a lot! |
+1 the cast to string option would be great |
@mrkamel I would be interested in helping out in implementing the |
Looks like this functionality can be achieved using Custom Operators search_scope :search do
attributes :number
generator :like_string do |column_name, raw_value|
pattern = quote("%#{raw_value}%")
"#{column_name} LIKE #{pattern}"
end
end
Book.search(number: {like_string: "123"}) |
Unfortunately, the example in #15 (comment) does not work for us, as we want a single search term that searches many columns, including numeric columns that we want to treat as strings and search using I've tried it by giving a custom column name, but this results in an error: attributes :my_string, :my_integer: 'convert(my_integer, CHAR)' Is there another way we can achieve this? |
hi @sudoremo ... i'm sorry, but #15 (comment) is still the status quo then. I still consider this feature to be added in the future, but can't give an ETA, but you're welcome to add a PR. Reopening this. |
@mrkamel Thank you, I'll attempt a PR if I find the time. |
As you see only comment field is looked up with LIKE statement. But I wanna find orders with numbers: 20590001, 20590002, 20590003 and so on.
The text was updated successfully, but these errors were encountered: