0

If provided with a text string e.g. "Tennis Account A33 Sydney AU" I need to find a db record that matches it. The "A33" part could be any characters.

I've created a model for my rails app called Descriptor which has the string column called pattern. The pattern can include the % character to show that any characters could be in this place in the string. e.g. "Tennis Account % Sydney AU"

So if the data in the table was as below and I wanted to find the descriptor which matched "Tennis Account A33 Sydney AU" it should return descriptor 1.

#<Descriptor id: 1, pattern: "Tennis Account % Sydney AU", ... >
#<Descriptor id: 2, pattern: "Tennis Account % Melbourne AU", ... >
#<Descriptor id: 3, pattern: "Tennis Account Sydney AU", ... >
#<Descriptor id: 4, pattern: "Jazz Flute Minions dney AU", ... >

......thousands more descriptors

How can this search query be written in postgres?

1 Answer 1

1

I've now realised this is just a LIKE query around the other way:

Descriptor.where('? LIKE pattern', "Tennis Account A33 Sydney AU").first
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.