0

I need to find in my rails app some data, first i search in rails model all data, and then via some field (which is getting from excel file) i need to find in in my array, but find as sql like %%, so not concrete, but like % % in sql. How to do this in ruby?

@suppliers = Supplier.find(:all)
supplier = @suppliers.find{|item| item['SUP_BRAND']=="*"+row[13]+"*"}

I do something like that, but that bad idea, is it any better way?

4 Answers 4

1

This should do the trick you want. We're matching against Regexp which is constructed from .* at the beginning and at the end which is the same as % in SQL and putting row[13] value between them:

@suppliers = Supplier.find(:all)
supplier = @suppliers.find{|item| item['SUP_BRAND'] =~ Regexp.new(".*#{row[13]}.*") }

Updated

I just checked and "regexp" syntax or how do you call it, will also work in this case:

item['SUP_BRAND'] =~ /.*#{row[13]}.*/
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand correctly, what you are trying to do is a partial match, right? Perhaps using regular expressions is the right way to go? Try this:

... item['SUP_BRAND'] =~ /row[13]/ ...

That will look for the text contained in row[13] within the string item['SUP_BRAND']. Try using Rubular to play around with regular expressions.

1 Comment

doesn't work, i need something this: item['SUP_BRAND'] like % row %
1

Looks like you're using ActiveRecord. You should just use the built-in querying:

Supplier.where("SUP_BRAND like ?", "%#{row[13]}")

Comments

0

The find method is an alias for the detect method and only returns the first item. I assume you want to return a collection of matching items?

@suppliers = Supplier.find(:all)
supplier = @suppliers.select { |item| item['SUP_BRAND'].include?(row[13]) }

or with a regex

supplier = @suppliers.select { |item| item['SUP_BRAND'] =~ /#{row[13]}/ }

But this very inefficient. What if you have hundreds or thousands of suppliers? Curious why this can't be written with straight-up ActiveRecord.

4 Comments

ok, than how to fetch id from supplier (getted via activerecord), if i write supllier.SUP_ID it gives me undefined method `SUP_ID'
Is it better to search in hash values? supplier have about 500 rows, ad every %like% is not fast as you think to
You've got something unconventional (in the Rails world) going on with SUP_BRAND and SUP_ID and this isn't the place to go into design philosophies. If you had a Supplier category (attribute or association) you could avoid the whole LIKE thing, perhaps.
I feel your pain, man. Sometimes it's like trying to hammer a square peg into a round hole. Still, though, the more you can offload to SQL queries and return only the data you need, the better. Good luck, sir!

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.