Closed krismartin closed 10 years ago
I'd go with an option like
increments :number, :use_inherited_class_name => true
or something like that.
Being able to define the class name is potentially dangerous as it can screw up the counts if you forget to change it, while use_inherited_class_name works automatically.
@suweller what do you think?
That's a better option! I like it.
I've got an issue when using the autoinc with sub-classes.
For example:
class Vehicle field :number, type: Integer increments :number end
class Car < Vehicle end
class Truck < Vehicle end
Because the increment! method uses class.model_name, it treats the sub-classes as two different entities. When I create new Car, increment! returns 1. When I create new Truck, increment! also returns 1 and so on. So the numbers are being duplicated.
This behaviour probably works for most cases, but in my case I need the number to be unique. Plus we also have a unique database constraint on the number field.
One possible option to fix this is to allow the sub-classes to define :model_name (or :class_name) in the increments declaration.
For example:
class Car < Vehicle increments :number, model_name: "Vehicle" end
I'd like to hear your thoughts before I create a pull request. What do you think?