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

How to force to use LIKE query on nonstring fields? #15

Open
biggless opened this issue Feb 2, 2015 · 8 comments
Open

How to force to use LIKE query on nonstring fields? #15

biggless opened this issue Feb 2, 2015 · 8 comments

Comments

@biggless
Copy link

biggless commented Feb 2, 2015

SELECT `orders`.* FROM `orders` WHERE `orders`.`deleted_at` IS NULL AND ((`orders`.`id` = 2059000 OR `orders`.`number` = 2059000 OR `orders`.`comment` LIKE '%2059000%'))

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.

@mrkamel
Copy link
Owner

mrkamel commented Feb 2, 2015

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

@biggless
Copy link
Author

biggless commented Feb 2, 2015

Great! Thanks a lot!

@westonganger
Copy link

+1 the cast to string option would be great

@westonganger
Copy link

westonganger commented Nov 26, 2020

@mrkamel I would be interested in helping out in implementing the :cast option. Any hints on where to start?

@westonganger
Copy link

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"})

@biggless biggless closed this as completed Sep 7, 2023
@sudoremo
Copy link

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 like.

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?

@mrkamel
Copy link
Owner

mrkamel commented Oct 31, 2023

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 mrkamel reopened this Oct 31, 2023
@sudoremo
Copy link

sudoremo commented Nov 1, 2023

@mrkamel Thank you, I'll attempt a PR if I find the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants