brendon / acts_as_list

An ActiveRecord plugin for managing lists.
http://brendon.github.io/acts_as_list/
MIT License
2.04k stars 355 forks source link

Behavior with DB default seems unclear #219

Closed maxim closed 8 years ago

maxim commented 8 years ago

Thank you very much for maintaining this gem. Have a use case that could maybe use a little love (I know it's been discussed before, but not enough it seems).

We have acts_as_list declared like this:

acts_as_list scope: [:active]

In DB position default is set to 1, but the problem is: each new record still goes to the bottom. Setting position to 1 explicitly, or using position_will_change! doesn't help (probably because it's equal to what's already set by default). A note in the README says this is normal, and refers to "recent discussions", however, that note was written in 2011, and I couldn't find any discussion that helps work around this, would appreciate some help.

I did also try adding add_new_at: :top, but with that the problem is that it always goes to the top, even when a position is provided.

I managed to hack together this workaround, the downside being that I have to use this method instead of standard AR create, and it has to do the creation in 2 DB interactions.

  class << self
    def create_at_position(params, &block)
      transaction do
        item = create(params, &block)
        item.move_to_top unless params[:position]
      end
    end
  end

Wonder if there is better way to do this that I could at least help document.

brendon commented 8 years ago

Hi @maxim :) Thanks for bring this up. I've created a PR with a test that confirms the bug: https://github.com/swanandp/acts_as_list/pull/220

If you come up with any insight as to how to work around this problem, please let me know :)

brendon commented 8 years ago

Updated the PR with a fix though it actually changes tested behaviour so I'm not quite sure wether to merge it or not.

brendon commented 8 years ago

Closed by #220.

brendon commented 8 years ago

0.8.0 has been released and includes this change.