Active record, where the request for a value inside the array

Question: Is it possible to create an area of ​​class methods that can query objects based on values ​​inside an array in a table? If so, how can I do this?

In my example, I have a table "wells" that has an array field called "well_tags". I want to build a query that returns all objects that have the specified value (for example, "ceramic") in the wells_tags array. The main request would be something like this:

@well = Well.all
@query = @well.where("well_tags contains ceramic")

And then the scope of the class methods will look something like this: with the parameter "well_tag_search" passed from the controller:

class Well < ActiveRecord::Base

def self.well_tag_filter(well_tag_search)
  if well_tag_search.present?
    where("well_tags contains ceramic")
  else
    Well.all
  end
end

, (. ), ... "", , 1 . sqlite () 4.0.

,

!

:

, , , select. Active Record, .

@well = Well.select
        { |well| if well.well_tags.present?
        then well.well_tags.include? ‘ceramic' end }

@well.class #=> array
+3
1

, Show.

Well.all Show.all?

0

All Articles