# Acts as list -- adding or moving new item to new location

**URL:** <https://www.ruby-forum.com/t/acts-as-list-adding-or-moving-new-item-to-new-location/109248>\
**Category:** Rails\
**Created:** [July 12, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/acts-as-list-adding-or-moving-new-item-to-new-location/109248 "2007-07-12T18:23:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [July 12, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/acts-as-list-adding-or-moving-new-item-to-new-location/109248/1 "2007-07-12T18:23:43Z")

</div>

right now i’m adding an item to my ‘list’ but i’m unsure how to move  
it to a certain spot in the list. specifically i want to put it at the  
bottom of the list. i assumed that if i did something like

parent.children \<\< new\_child

then the ‘position’ field in the child table would be given some  
sensible default by rails but instead it’s set to NULL.

of course there’s a bunch of ugly ways i can make this work but i’m  
wondering what would be the best/cleanest way…?

thanks in advance,  
mike

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [July 13, 2007, 4:07am UTC](https://www.ruby-forum.com/t/acts-as-list-adding-or-moving-new-item-to-new-location/109248/2 "2007-07-13T04:07:56Z")

</div>

You must be doing something wrong if position is not being set by  
acts\_as\_list.

Here’s an example:

---- migrations  
create\_table :parents, :force =\> true do |t|  
end

create\_table :children, :force =\> true do |t|  
t.column :parent\_id, :integer  
t.column :name, :string  
t.column :position, :integer  
end

---- models  
class Parent \< ActiveRecord::Base  
has\_many :children, :order =\> :position  
end

class Child \< ActiveRecord::Base  
belongs\_to :parent  
acts\_as\_list :scope =\> parent  
end

---- some methods on acts\_as\_list you can use to manipulate the list  
decrement\_position  
first?  
higher\_item  
in\_list?  
increment\_position  
insert\_at  
last?  
lower\_item  
move\_higher  
move\_lower  
move\_to\_bottom  
move\_to\_top  
remove\_from\_list

---- so you can do stuff like  
puts parent.children[0].first? #=\> true  
parent.children[0].move\_lower  
parent.children[2].move\_to\_top
