# Acts\_as\_list: update all items?

**URL:** <https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415>\
**Category:** Rails\
**Created:** [November 27, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415 "2006-11-27T19:55:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 27, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415/1 "2006-11-27T19:55:29Z")

</div>

Hi there

I have the models product and images, where a product may have as many  
images as it wants. I already have several products and images, and now  
I want to make the listable. So I created a migration that adds a field  
“position” to the images table.  
So far, so good. But I also want the migration to update the position  
fields of every image, so the images of product X have the numbers 1, 2,  
3 etc. in their position fields.

How can I do that?

Thanks for help,  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 28, 2006, 9:00am UTC](https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415/2 "2006-11-28T09:00:21Z")

</div>

Joshua M. wrote:

> Hi there
> 
> I have the models product and images, where a product may have as many  
> images as it wants. I already have several products and images, and now  
> I want to make the listable. So I created a migration that adds a field  
> “position” to the images table.  
> So far, so good. But I also want the migration to update the position  
> fields of every image, so the images of product X have the numbers 1, 2,  
> 3 etc. in their position fields.
> 
> How can I do that?
> 
> Thanks for help,  
> Josh

You can simply embed ruby in your migration:

def self.up  
add\_column :images, :position, :integer  
Product.find(:all) do |p|  
p.images.each\_with\_index |img, i|  
img.update\_attribute(:position, i)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 28, 2006, 9:08am UTC](https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415/3 "2006-11-28T09:08:10Z")

</div>

Alex W. wrote:

> Product.find(:all) do |p|  
> ^  
> ain’t that with .each?

Maybe I have been reading find() do and writing find().each do…

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 28, 2006, 9:23am UTC](https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415/4 "2006-11-28T09:23:30Z")

</div>

Phlip wrote:

> Alex W. wrote:
> 
> > Product.find(:all) do |p|  
> > ^  
> > ain’t that with .each?
> 
> Maybe I have been reading find() do and writing find().each do…
> 
> –  
> Phlip  
> [Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

heh, um yeah. it’s late 😛

def self.up  
add\_column :images, :position, :integer  
Product.find(:all).each do |p|  
p.images.each\_with\_index |img, i|  
img.update\_attribute(:position, i)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 28, 2006, 9:30am UTC](https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415/5 "2006-11-28T09:30:18Z")

</div>

Lol thank you guys, I nearly became nuts because of the missing each! %)  
Anyway, there’s still a missing “do” somewhere… Anyone can find it?  
😉

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 28, 2006, 9:40am UTC](https://www.ruby-forum.com/t/acts-as-list-update-all-items/77415/6 "2006-11-28T09:40:16Z")

</div>

Joshua M. wrote:

> Lol thank you guys, I nearly became nuts because of the missing each! %)  
> Anyway, there’s still a missing “do” somewhere… Anyone can find it?  
> 😉

Everybody go do something physical to burn off the coffee…

…then go to bed!

Discretion is the better part of valor! Live to program another day!

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!
