ActiveRecord find starts with

前端 未结 8 1917
走了就别回头了
走了就别回头了 2021-01-30 05:33

Really simple question - how do I do a search to find all records where the name starts with a certain string in ActiveRecord. I\'ve seen all sorts of bits all over the internet

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 05:57

    Dave Sag, I guess the first part of your code should be

    class User
      scope :name_starts_with, (lambda do |str|
                                  {:conditions => ['lower(name) like ?', "#{str.downcase}%"]}
                                end )
    end
    

提交回复
热议问题