regmiprem
regmiprem

Reputation: 735

Searching MongoDB with Rails

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.

Upvotes: 1

Views: 1062

Answers (1)

user176060
user176060

Reputation:

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.

Upvotes: 4

Related Questions