Handle multiple lists: move an item from one list to another


#1

How do I manage multiple lists?

This is what I need. Sometimes an item needs to be moved from one list
to another.

class Item < ActiveRecord::Base
belongs_to :group
acts_as_list :scope => :group
end

class Group < ActiveRecord::Base
has_many :items, :order => “position”
end

Now, sometimes, I may need to change an item from one group to another.
But when I do that, the position number is not corrected. I end up
having multiple items in a group with the same position.

What is the correct way to handle something like this?
Thanks for your help.
Arpan


#2

I’ve had the same problem. It seems that acts_as_list doesn’t updates
positions when scope changes.
I tried to set up a callback method before_validation_on_update in the
model, like so :

class Item < ActiveRecord::Base
belongs_to :group
acts_as_list :scope => :group

before_validation_on_update :reorder_positions

private

def reorder_positions
	@updated_group_id = params[:person][:group_id]
	unless @updated_group_id == self.group_id
		self.move_to_bottom # reorder original list
		params[:item][:position] = 

Group.find_by_id(@updated_group_id).items.length + 1
end
end
end

Unfortunately it doesn’t work, somehow the model throws an error on
accessing the params hash.

So I solved this at the controller level, in the update method.

It works but I’m concerned about breaking the MVC, such a reordering
should happen in the model.

Bernard.