What's the best way to include a LIKE clause in a Rails query?

前端 未结 3 857
醉酒成梦
醉酒成梦 2020-12-30 00:49

What\'s the best way to include a LIKE clause in a Rails query i.e. something along the lines of (the completely incorrect):

 Question.where(:content => \         


        
3条回答
  •  梦谈多话
    2020-12-30 01:29

    If you want really sexy conditions and and don't have problems with external dependencies, I highly recommend MetaWhere and it's successor Squeel:

    # MetaWhere
    Question.where(:content.like => '%farming%')
    
    # MetaWhere with operators overloaded
    Question.where(:content =~ '%farming%')
    
    # Squeel
    Question.where { :content.matches => '%farming%' }
    
    # Squeel with operators overloaded
    Question.where { :content =~ '%farming%' }
    

提交回复
热议问题