ranked-model
ranked-model copied to clipboard
Error on trying to sort records with row_order = null
class AddRowOrderToImages < ActiveRecord::Migration
def change
add_column :images, :row_order, :integer
end
end
class Image < ActiveRecord::Base
# ...
include RankedModel
ranks :row_order, with_same: [:attachable_id, :attachable_type]
end
@image.update_attribute :row_order_position, 0
# raises NoMethodError (undefined method `-' for nil:NilClass):
I've solved it migrating to :null => false, :default => 0 for row_order column.
Solutions:
- update README with example migration with specified :null => false, :default => 0
- respect null value during row_order recalculations.
@stgeneral I believe the docs should just be updated, confirmed.
This is at odds with this comment: https://github.com/mixonic/ranked-model/issues/32#issuecomment-56923115
The above linked fixed an issue for me whereby I had my column defaulting it to 0, but my ordering was screwing up on insertion (where no :row_order_position was supplied). Defaulting to null seems to have fixed the issue for me.
I'm not sure if setting a default value for that column is supported from an ideological point of view unless you set it to the maximum or minimum value?
Having a default column value is not allowed. We check for this now.