# Saving a collection

**URL:** <https://www.ruby-forum.com/t/saving-a-collection/51458>\
**Category:** Rails\
**Created:** [December 31, 2005, 5:49pm UTC](https://www.ruby-forum.com/t/saving-a-collection/51458 "2005-12-31T17:49:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [December 31, 2005, 5:49pm UTC](https://www.ruby-forum.com/t/saving-a-collection/51458/1 "2005-12-31T17:49:55Z")

</div>

Reading the Agile book, I can’t find a single command to save a  
collection.  
The save command seems to operate on only individual objects.

Lets say I have a collection that I have received from a find\_all  
command.  
I want to go through and change some attributes in various objects in  
the  
collection. Then I want to resave the whole collection back to the  
database. I don’t think the update\_all command is right for this, as it  
understand it…

Do I just have to iterate through the collection, issuing a save command  
on  
each object?

Thanks for any advice…

Shelby

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [December 31, 2005, 8:53pm UTC](https://www.ruby-forum.com/t/saving-a-collection/51458/2 "2005-12-31T20:53:55Z")

</div>

On 12/31/05, Shelby W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> each object?

To the best of my knowledge, there’s not a quicker way than:  
@your\_collection.each {|r| r.save}

On the other hand, you probably also need to make sure the records are  
valid, so…  
good\_to\_go = true  
@collection.each do |record|  
good\_to\_go = false unless record.save  
end  
flash[:notice] = “All records saved. Lunch break!” if good\_to\_go  
etc, etc, etc.

I have a lot of this stuff in my Rails apps, so hopefully someone will  
come along with a better way.

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [December 31, 2005, 11:30pm UTC](https://www.ruby-forum.com/t/saving-a-collection/51458/3 "2005-12-31T23:30:17Z")

</div>

> Do I just have to iterate through the collection, issuing a save command  
> on

> > each object?
> 
> To the best of my knowledge, there’s not a quicker way than:  
> @your\_collection.each {|r| r.save}

Thanks, Wilson - I couldn’t figure out any other way to do it as  
well…

Shelby

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [January 1, 2006, 7:45pm UTC](https://www.ruby-forum.com/t/saving-a-collection/51458/4 "2006-01-01T19:45:42Z")

</div>

> On the other hand, you probably also need to make sure the records are  
> valid, so…  
> good\_to\_go = true  
> @collection.each do |record|  
> good\_to\_go = false unless record.save  
> end  
> flash[:notice] = “All records saved. Lunch break!” if good\_to\_go  
> etc, etc, etc.

You could also do it like this:

good\_to\_go = @collection.all? { |r| r.save }

–

Dan

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [January 1, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/saving-a-collection/51458/5 "2006-01-01T22:31:52Z")

</div>

On 1/1/06, Dan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> good\_to\_go = @collection.all? { |r| r.save }

Nice one. The best posts on this list are the ones that make me feel  
stupid.  
I’m going to rewrite some actions now. 🙂
