Use LIKE/regex with variable in mongoid

后端 未结 5 2041
悲哀的现实
悲哀的现实 2021-02-01 18:40

I\'m trying to find all documents whose text contains the word test. The below works fine:

@tweets = Tweet.any_of({ :text => /.*test.*/ })

H

5条回答
  •  春和景丽
    2021-02-01 18:45

    searchterm = (params[:searchlogparams][:searchterm])
    @tweets = Tweet.any_of({ :text => Regexp.new ("/.*"+searchterm+".*/") })
    

    or

    searchterm = (params[:searchlogparams][:searchterm])
    @tweets = Tweet.any_of({ :text => /.*#{searchterm}.*/ })
    

提交回复
热议问题