acts_as_list upper_limit on position

269 Views Asked by At

I am using acts_as_list on my model.I have a position column on that model for which I don't wan't the model object to be saved to the database if the position is more than my upper-limit.(I tried using rails validation for position, but apparently it seems that rails validation runs first then acts_as_list does it's job to update(increment) the position and save it in db.

Is something like this possible with acts_as_list scope: :widgets, {0 < :position <= 2} I went through their documentation, but couldn't find anything.

How can I do it in Rails.

Any help will be much appreciated. please feel free to ask me for more info if you need.

2

There are 2 best solutions below

0
On

I think you need to do something like this

acts_as_list scope: :widgets,  if: 'position <= 2'
0
On

acts_as_list doesn't support this natively. You could try adding a validation based on there being no more than x records with the matching scope?

validates :maximum_records

private

def maximum_records
  if where([[scope conditions]]).count > something
    errors.add :base, 'too many records'
  end
end