Getting record where the first set of characters in attribute matches

时间:2016-10-19 13:38:29

标签: ruby-on-rails ruby rails-activerecord

I have a model with a string attribute, the attribute can have values 'abc', 'def', 'ghi' or 'jkl' + count I need to get all records with the value 'jkl' + count. I was hoping something like:

Model.where(status[0..2]: 'jkl')

Would work, but it seems like it doesn't, is there a good way for me to do this?

1 个答案:

答案 0 :(得分:2)

You can use LIKE query with wildcards

Model.where('status LIKE ?', 'ijk%')