rails 使用mongoid进行模糊查询

xiaoxiao2021-02-28  157

使用mongoid进行查询时查询的字段需要使用正则表达进行查询 例如查询图书名字中包含ruby的书籍可以使用

Book.where(name: /ruby/ )

一句话进行多条件模糊搜索

def index conditions = {} %w{name author book_type }.each{|attr| conditions[attr]=Regexp.new(params[attr]) unless params[attr].blank? } @books = Book.where(conditions).order("updated_at DESC") end
转载请注明原文地址: https://www.6miu.com/read-34863.html

最新回复(0)