ranked-model icon indicating copy to clipboard operation
ranked-model copied to clipboard

Error on trying to sort records with row_order = null

Open stgeneral opened this issue 10 years ago • 3 comments

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 avatar Mar 28 '14 14:03 stgeneral

@stgeneral I believe the docs should just be updated, confirmed.

mixonic avatar Mar 30 '14 21:03 mixonic

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.

glennr avatar Dec 20 '18 05:12 glennr

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?

brendon avatar Dec 20 '18 07:12 brendon