Forum: Ruby on Rails handle multiple lists: move an item from one list to another

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Arpan (Guest)
on 2006-03-23 20:08
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

class Group < ActiveRecord::Base
  has_many :items, :order => "position"

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.
Bernard D. (Guest)
on 2006-04-02 14:04
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


	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

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.

This topic is locked and can not be replied to.