开发者

Model has a field which is an array of strings, how can I find all records that contain a certain string in the array?

My model has a field c开发者_C百科alled user_ids, which is an array of strings. How can I find all of the records which include a certain string, string_1, in user_ids?

I'm using Rails 3.0.1 & Ruby 1.9.2p0.


You can use something like this scope:
scope :some_scope_name, lambda {|user_id| where(["user_ids RLIKE ?", user_id])}
I used this type of scope in project, where I have to find all records with list_ids column match some criteria.

.. or you can use find_by_sql method with same SQL: ["user_ids RLIKE ?", criteria]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜