Emre Emre - 1 year ago 101
SQL Question

Rails LIKE - Query not working properly

I have a house model and business_model[] column. This column can be set as, "awake", "sleep" or "awake, sleep" with radio buttons, all string. I would like to get all the houses include business_model as "awake" for instance.

@house = (House.all).uniq.where("business_model like ?", "sleep")

I have tried that code. It works only if column is just "sleep". When it is "awake, sleep" it returns nil.

Why is that?

Answer Source

You need to throw some wild cards in there. Try where("business_model like ?", "%sleep%"). Note that this will also catch things like sleeping and asleep.

You may want to consider using a JSON database column that gives you more control over the querying.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download