【问题标题】:Model has a field which is an array of strings, how can I find all records that contain a certain string in the array?模型有一个字段,它是一个字符串数组,我怎样才能找到数组中包含某个字符串的所有记录?
【发布时间】:2011-05-08 05:08:56
【问题描述】:

我的模型有一个名为user_ids 的字段,它是一个字符串数组。如何在user_ids 中找到包含某个字符串string_1 的所有记录?

我正在使用 Rails 3.0.1 和 Ruby 1.9.2p0。

【问题讨论】:

  • ActiveRecord 还是 MongoID?如果是 AR,为什么要使用序列化而不是 1:n 关系?

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

您可以使用类似这样的范围:
范围 :some_scope_name, lambda {|user_id| where(["user_ids RLIKE ?", user_id])}
我在项目中使用了这种类型的范围,我必须找到所有具有 list_ids 列的记录符合某些条件。

.. 或者您可以使用具有相同 SQL 的 find_by_sql 方法:["user_ids RLIKE ?", criteria]

【讨论】:

    猜你喜欢
    • 2014-05-27
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多