Rails Tire search on ActiveRecord:Relation on multiple fields

175 views Asked by At

I have a collection of ActiveRecord models queried from the database. I would like to perform a Tire search on that Relation object returned from ActiveRecord, e.g:

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

I would like to query that ActiveRecord:Relation using a single or multiple fields. Which fields I would like to query (:attr_1 or :attr_2 or :attr_3), is dynamic, so it would be nice if I could send a Hash to the query method.

Is something like this possible?

1

There are 1 answers

1
Automatico On

Not 100% sure what you are asking, but if it is "I want to find the model_2s that have parameters x, y and z equals to something specific", then you can use the 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" }

If this is not what your are asking, please rewrite your question a bit.