Reputation: 735
here my code in model is
def self.search(search)
if search
where(name: /#{Regexp.escape(search)}/i)
else
scoped
end
end
Now i want to add another field also to search like
where(price: /#{Regexp.escape(search)}/i)
So my query should search two field like
where(name: /#{Regexp.escape(search)}/i) (or) where(price: /#{Regexp.escape(search)}/i)
How to add this two field in mongodb like or . Working example is accepted. Since i have less knowledge about mongodb.
Upvotes: 0
Views: 125
Reputation: 1705
If you are using Mongoid 3, you can write your query like this:
self.or({name: /#{Regexp.escape(search)}/i}, {price: /#{Regexp.escape(search)}/i})
If you are using something other than Mongoid 3 (like MongoMapper or Mongoid 2), please give the name and version number.
Source: Selection syntax docs for Mongoid 3 are here.
Upvotes: 1
Reputation: 2653
Try this
where('$or' => [{"name" => "/#{Regexp.escape(search)}/i"}, {"price" => "/#{Regexp.escape(search)}/i"}])
Upvotes: 0