Forum: Ruby on Rails saving a collection

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.
78e1e63b1c4789fb757317fe31d362b3?d=identicon&s=25 Shelby Westman (Guest)
on 2005-12-31 17:49
(Received via mailing list)
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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2005-12-31 20:53
(Received via mailing list)
On 12/31/05, Shelby Westman <shelby.westman@gmail.com> 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.
78e1e63b1c4789fb757317fe31d362b3?d=identicon&s=25 Shelby Westman (Guest)
on 2005-12-31 23:30
(Received via mailing list)
>  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
Ea627ef000ec92c6cdd5a4c14075e740?d=identicon&s=25 Dan Kubb (Guest)
on 2006-01-01 19:45
(Received via mailing list)
> 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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-01 22:31
(Received via mailing list)
On 1/1/06, Dan Kubb <dan.kubb@autopilotmarketing.com> 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. :)
This topic is locked and can not be replied to.