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]
精彩评论