ActiveRecord find starts with

If you’re looking to do the search in the database then you’ll need to use SQL.

And, of course, you’ll need to do the search in the database otherwise you need to load all the objects into Ruby (which isn’t a good thing).

So, you will need something like

MyModel.find(:all, :conditions => ["field LIKE ?", "#{prefix}%"])

where prefix is a variable with the string you’re looking for.

In Rails 3.0+ this becomes:

MyModel.where("field LIKE :prefix", prefix: "#{prefix}%")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)