module ActiveRecord::JsonHasMany

Constants

VERSION

Public Instance Methods

json_has_many(many, class_name: nil) click to toggle source
# File lib/active_record/json_has_many.rb, line 7
def json_has_many(many, class_name: nil)
  one = many.to_s.singularize
  one_ids = :"#{one}_ids"
  one_ids_equals = :"#{one_ids}="
  class_name ||= one.classify
  many_equals = :"#{many}="
  many_eh = :"#{many}?"

  serialize one_ids, JSON

  include Module.new {
    define_method one_ids do
      super() || []
    end

    define_method many do
      class_name.constantize.where(id: send(one_ids))
    end

    define_method many_equals do |collection|
      send one_ids_equals, collection.map(&:id)
    end

    define_method many_eh do
      send(one_ids).any?
    end
  }
end
where_json_array_includes(pair) click to toggle source
# File lib/active_record/json_has_many.rb, line 36
def where_json_array_includes pair
  field, value = *pair.to_a.first
  where("#{field} RLIKE '[[:<:]]#{value}[[:>:]]'")
end