# How to make a copy of a collection in the update controller method

**URL:** <https://www.ruby-forum.com/t/how-to-make-a-copy-of-a-collection-in-the-update-controller-method/155068>\
**Category:** Rails\
**Created:** [December 7, 2008, 10:48am UTC](https://www.ruby-forum.com/t/how-to-make-a-copy-of-a-collection-in-the-update-controller-method/155068 "2008-12-07T10:48:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 7, 2008, 10:48am UTC](https://www.ruby-forum.com/t/how-to-make-a-copy-of-a-collection-in-the-update-controller-method/155068/1 "2008-12-07T10:48:14Z")

</div>

I can’t figure this out.

Imagine a Ticket object and this object has\_many LineItems

Now, my ticket edit form will allow user to modify Ticket attributes  
and add/modify/delete LineItems at will.

So the edit action sends the LineItems to the form and what comes back  
could be LineItems in any which way/order or even no LineItems.

With that in mind, I thought, okay, well, I better hold on to the  
original list of line items somehow in case validation of the Ticket  
item fails. (no validation of the individual line items, we just throw  
away the ones that have a blank “item” attribute, but the main ticket  
attributes do have some validation happening)

In the update controller method I wanted to:

store original line\_items in an array or maybe a hash doesn’t matter

take the new line items from the form hash and “build” them on to the  
ticket  
so, now I have a line\_items list that is the original list + what just  
got added from the form

then, if the Ticket validates,  
take that original line\_items list and delete from ticket’s current  
line\_items which again is original list + what got added from the form

the problem is, no matter what I do, I can’t seem to save the  
original\_line\_items so they don’t get touched when processing the  
line\_items from the form

At the very beginning of the update controller method I have:

@ticket = Ticket.find(params[:id])  
@line\_items = @ticket.line\_items

I think my problem is that I don’t understand what is happening here.  
I thought this said:

1. ok, look up the ticket you want to work on, and here’s the original  
line\_items for the ticket.
2. now manipulate those via the params from the form …

I tried stuff like:

orig\_line\_items = @ticket.line\_items

orig\_line\_items = Array.new(@line\_items)

but no luck, no matter what I do, as soon as I start working with  
@line\_items and the params from the form, orig\_line\_items seems to get  
changed

I know this because if I start with a ticket with zero LineItems (from  
script/console I look up the ticket, assign its line items to an array  
called orig\_line\_items and then say “is that array empty”, answer =\>  
true)

but in my controller, even if I added no line\_items via the form

@orig\_line\_items.empty? ----- this is always false, 100% of time, no  
matter what happened with the form

I’m not understanding a basic form processing concept here I think.

Here’s an example of what the form returns, this is good, looks  
exactly as I expect/want

{“commit”=\>“Update Ticket”,  
“ticket”=\>{“status”=\>“new”,  
“title”=\>“tested”,  
“owner”=\>“1”,  
“email”=\>“removed\_email\_address@domain.invalid”},  
“id”=\>“7”,  
“ticket\_extra”=\>{“uploaded\_data”=\>""},  
“line\_items”=\>{“0”=\>{“date\_created”=\>Sat,  
06 Dec 2008 20:26:29 +0000,  
“is\_hidden”=\>“0”,  
“item”=\>“ff”},  
“1”=\>{“date\_created”=\>Sat,  
06 Dec 2008 20:32:41 GMT +00:00,  
“is\_hidden”=\>“0”,  
“item”=\>“dd”}}}

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 7, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/how-to-make-a-copy-of-a-collection-in-the-update-controller-method/155068/2 "2008-12-07T12:11:23Z")

</div>

On 6 Dec 2008, at 23:42, jim wrote:

> 

There’s two levels of trickery here. First @ticket.line\_items isn’t an  
array. At this point the line items have not been loaded. It’s a  
magical association proxy. Even if you were to sidestep that, you’re  
still not copying the array. To put things another way, is

a = [1,2,3]  
b = a  
Then b and a are exactly the same array:  
b \<\< 4  
a #=\> [1,2,3,4]

> I tried stuff like:
> 
> orig\_line\_items = @ticket.line\_items
> 
> orig\_line\_items = Array.new(@line\_items)  
> Creating a new Array like that (or duping the original array) should  
> work. I suspect that since you seemed to have been changing stuff  
> slightly in desperation you did something else at the same that  
> clouded the issue.

Fred

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 10, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-make-a-copy-of-a-collection-in-the-update-controller-method/155068/3 "2008-12-10T20:02:02Z")

</div>

Exactly, after more tinkering and such I made it work by copying to an  
array.

Thanks for taking the time to read my post,

On Dec 7, 6:10 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
