1

I am using ruby 1.9.3p194 and rails 3.2.5 when i do

@authors = Author.all

it works fine by outputing all name from my database. But

@authors = Author.where("name like ?", "%#{params[:q]}%")

is not working. I am using Mongodb. I want to output the name(Ram) when i input R. What could be the solution?


By this method i can search params[:q] through my field :name only. How can i add other field name also such i want to seach this params[:q] in text field :role , :email also at once? I used || but it gives error. I have to write this in comment but since my comment list is long . I ask this question here. Sorry for that.

1 Answer 1

4

MongoDB doesn't use "LIKE" queries as in SQL. You (can) use regular expressions: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

To solve your specific problem, you could use @authors = Author.where(name: /#{params[:q]}/), but I'm not entirely sure if this can be a security issue or not.

Sign up to request clarification or add additional context in comments.

7 Comments

/#{Regexp.escape(params[:q])}/ might server you better. And it is worth noting that you'll be limited to the regexes that MongoDB supports rather than Ruby's.
DO u have any idea that search condition works for capital letter also. I mean to say( if a then it displays all associated data including capital letter also) for ex a->apple,Aeroplane
To make it case insensitive, add a "i" to the end, like this: /#{Regexp.escape(params[:q])}/i
can we code it such that if one data is choosen then it will not display second time. for example i choose m(results mongo monkey) and mongo is chhosen there. and in second time also if i choose m then only monkey should be displayed . I am using jquery token. might i have to change in js or here i am confused.
@regmiprem probably, but that should be another question
|

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.