validates error #49

Closed
venomizer opened this Issue Aug 7, 2012 · 2 comments

Projects

None yet

3 participants

@venomizer

I don't know if it is an issue with acts_as_list or just my code but i ran into the problem that if i have a validates :uniqueness on the position column, an error would come up when creating a new list_item saying that the position was not unique. I removed the validation and my app works fine now but thought it might be worth mentioning.

@RichardBradley

I had a similar problem -- my position column is marked as unique in the database, and the "move_lower" implementation causes unique constraint errors.

I was able to fix this with the following monkey patch (this doesn't fix all methods, e.g. "move_to_bottom" / "move_to_top" are still broken):

module ActiveRecord::Acts::List::InstanceMethods
  # See acts_as_list/active_record/acts/list.rb
  def move_lower
    return unless other = lower_item
    swap_position_with other
  end

  def move_higher
    return unless other = higher_item
    swap_position_with other
  end

  def swap_position_with other_item
    acts_as_list_class.transaction do
      other_position = other_item.send(position_column)
      other_item.update_attributes! position_column => -1
      self_position = send(position_column)
      update_attributes! position_column => other_position
      other_item.update_attributes! position_column => self_position
    end
  end
end
@swanandp
Owner

@venomizer @RichardBradley :

Closing this because this issue has been fixed in later pull requests. Can you please update your gems and let us know if it's still an issue?

@swanandp swanandp closed this Feb 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment