【问题标题】:Rails Tire search on ActiveRecord:Relation on multiple fields在 ActiveRecord 上的 Rails Tire 搜索:多个字段上的关系
【发布时间】:2013-12-17 04:27:51
【问题描述】:

我有一组从数据库中查询的 ActiveRecord 模型。我想对从 ActiveRecord 返回的 Relation 对象执行轮胎搜索 on,例如:

class Model_1
  has_many :model_2
end

class Model_2
  attr_accessible :attr_1, :attr_2, :attr_3
  belongs_to :model_1
end

class Model2Controller
  def index
    @model_2s = @model_1.model_2s

    # How can I query single or multiple fields here?
    @model_2s.search :query => { :attr_1 => params[:q] }
  end
end

我想使用单个或多个字段查询 ActiveRecord:Relation。我要查询的字段(:attr_1:attr_2:attr_3)是动态的,所以如果我可以向查询方法发送 Hash 就好了。

这样的事情可能吗?

【问题讨论】:

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


    【解决方案1】:

    不是 100% 确定您要问什么,但如果是“我想找到参数 x、y 和 z 等于特定值的 model_2s”,那么您可以使用Ruby Array #Select

    model_2s_you_care_about = @model_2s.select{ |e| e.attr_foo == "something" && e.attr_bar == "whatever" && e.attr_baz == "the last thing" }
    

    如果这不是您的要求,请稍微改写您的问题。

    【讨论】:

    • 我的意思是我想在 Model_2 的特定实例的嵌套模型上使用轮胎弹性搜索
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2012-02-12
    • 2012-08-02
    • 2021-07-13
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多