David Black and I have been working together to bring collections-based Create/Read/Update/Delete routines in to the next release of Rails. To summarize the progress so far: There is now an auto-index feature built in to all FormHelper objects such that: # The value of @student.id = 5 <%= text_field "student[]", "first_name" %> Will produce: <input type="text" id="student_5_first_name" name="student[5][first_name]" ... > This feature eliminates the need to have the " ''index'' => @student.id " option tacked on to the end of each form helper method. (Thanks, D. Black) In addition, the following methods are available as part of ActiveRecord''s base to aid in the handling of these "collections" of ActiveRecord objects: Person.find_collection( [1, 2, 3] ) => [ array of Person objects ] Person.update_collection( { 1 => { ''first_name'' => ''Duane'', ''last_name'' => ''Johnson'' }, 2 => { ''first_name'' => ''Eran'', ''last_name'' => ''Greenburg'' } ) => [ array of updated (and saved) Person objects 1 and 2 ] Person.create_collection( [ {''first_name'' => ''Duane'', ''last_name'' => ''Johnson'' }, {''first_name'' => ''Eran'', ''last_name'' => ''Greenburg'' } ] ) => [ array of newly created Person objects ] The create_collection method can also receive a hash, and optionally force the created objects to have IDs equal to the hash keys. Enjoy! -- Duane Johnson
Ah, great. That saves me from implementing this. Thanks, guys, it was on my todo list (though not Tada list). Quoting Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Person.update_collection( { 1 => { ''first_name'' => ''Duane'', > ''last_name'' => ''Johnson'' }, 2 => { ''first_name'' => ''Eran'', ''last_name'' > => ''Greenburg'' } ) > => [ array of updated (and saved) Person objects 1 and 2 ]Does this work if the hash keys are strings? It ought to. Tim. -- Tim Bates tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org
Duane Johnson
2005-Jan-31 23:02 UTC
Re: List-like forms now have CRUD methods to support them
Just to follow up with this since the 0.9.5 release: The method names no longer have the _collection suffix, since David Hansson prefers to have all of these collection-based CRUD methods coincide with the simpler ''update'', ''find'', ''create'', ''build'', and ''delete'' methods we''re all used to. These methods can be called with either a single ActiveRecord object (such as Person.update {hash of field => value pairs}) or with several ActiveRecord objects in an array (e.g. Person.find 1, 2, 3 or Person.find [1, 2, 3]). Thanks David! Duane Johnson (canadaduane) On Tue, 25 Jan 2005 00:43:18 +0000, Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> David Black and I have been working together to bring > collections-based Create/Read/Update/Delete routines in to the next > release of Rails. To summarize the progress so far: > > There is now an auto-index feature built in to all FormHelper objects such that: > # The value of @student.id = 5 > <%= text_field "student[]", "first_name" %> > > Will produce: > > <input type="text" id="student_5_first_name" > name="student[5][first_name]" ... > > > This feature eliminates the need to have the " ''index'' => @student.id > " option tacked on to the end of each form helper method. (Thanks, D. > Black) > > In addition, the following methods are available as part of > ActiveRecord''s base to aid in the handling of these "collections" of > ActiveRecord objects: > > Person.find_collection( [1, 2, 3] ) > => [ array of Person objects ] > > Person.update_collection( { 1 => { ''first_name'' => ''Duane'', > ''last_name'' => ''Johnson'' }, 2 => { ''first_name'' => ''Eran'', ''last_name'' > => ''Greenburg'' } ) > => [ array of updated (and saved) Person objects 1 and 2 ] > > Person.create_collection( [ {''first_name'' => ''Duane'', ''last_name'' => > ''Johnson'' }, {''first_name'' => ''Eran'', ''last_name'' => ''Greenburg'' } ] ) > => [ array of newly created Person objects ] > > The create_collection method can also receive a hash, and optionally > force the created objects to have IDs equal to the hash keys. > > Enjoy! > > -- Duane Johnson >