sqlite3 on rails: create_table using collate nocase

后端 未结 2 1812
孤城傲影
孤城傲影 2021-01-24 02:13

Using rails 4.2.0 with ruby v2.3.0p0

I want to create indexes that are case insensitive since most of my searches are case insensitive and I don\'t want to have to do a

2条回答
  •  轮回少年
    2021-01-24 02:51

    For people searching, if 'collate' is still not available in your version of rails (you'll get "Unknown key: :COLLATE"), then you have to create the index manually:

    execute("CREATE INDEX 'index_events_on_name' ON 'events' ('name' COLLATE NOCASE);")
    

    Then to search using the index, you can do:

    Event.where("name collate nocase == ?",name)
    Event.where("name LIKE ?",name)
    

    The 'find_by_name' won't be case insensitive. I believe you can do this by putting the 'collate nocase' on the create table instead of in the index (this will also need to use an "execute()" call. This means that you will always be forced to match case-insensitive, i.e., this will also be case-insensitive:

    Event.where("name == ?",name)
    

提交回复
热议问题