suweller / mongoid-autoinc

⬆ Auto incrementing fields for Mongoid documents
MIT License
62 stars 44 forks source link

It is possible to seed by Scope #37

Closed anquegi closed 5 years ago

anquegi commented 5 years ago

Recently I discovered this gem, and I want to change my bad method of autoincrement one mongo counter to use this. So the chage it works well adding this:

  field :number, type: Integer
  increments :number, scope: :customer_id

The old counter is stored in reference variable; I did this:

  before_create :assign_reference, unless: :reference
  field :reference, type: Integer
def assign_reference
    self.reference = customer.detections_counter
  end

The detections counter is a counter of the items created by a customer, so every time that I created a new object, I count mongo objects and return that number plus one.

If I change this to mongoid-autoinc, I need to seed each customer with a different value. How can I achieve that? the value should be the items created by the customer.

If I try something with the seed, the seed cannot be an anonymous function.

suweller commented 5 years ago

I think I understand, if my recollection is correct this is indeed currently not supported by mongoid-autoinc. Feel free to create a pull-request, so I can have a more clear idea of what you mean, and try to include it in the gem.