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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
01dcd647d65f131df96e3c0bc969dcb4?d=identicon&s=25 Arpan (Guest)
on 2006-03-23 19: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
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
30494933a87bf0f027adc26b9c3238b2?d=identicon&s=25 Bernard Dubuisson (bernardub)
on 2006-04-02 12: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

	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.
This topic is locked and can not be replied to.