trackoid icon indicating copy to clipboard operation
trackoid copied to clipboard

It would be nice to allow aggregate key to be of different data type

Open twoixter opened this issue 14 years ago • 1 comments

Currently, an aggregation key must be a String. For example

aggregate :browsers do |b|
    b.id   # <= This gets automatically casted to a string by calling ".to_s"
end

It would be nice to allow for BSON::ObjectID or DateTime keys. However, this would not be possible without using Inheritance for the Aggregation class model. I can replace the :ns (namespace) field and use Inheritance instead (automatically using the :_type field).

twoixter avatar Jul 18 '10 01:07 twoixter

Wouldn't it be possible to do something like this?

Mongoid::Tracking::Aggregates

def aggregate(args*, &block)
  *omitted*
  name = args.shift
  define_aggregate_model(args.extract_options!) if aggregate_klass.nil?
  *omitted*
end
def define_aggregate_model(args)
  field :key, args
end

And then in the model:

aggregate :sources, type: Symbol do |key|
  key.present? ? key.to_s : nil
end

mattiassvedhem avatar Sep 08 '12 17:09 mattiassvedhem