# Collection management on form updates - problem with updatin

**URL:** <https://www.ruby-forum.com/t/collection-management-on-form-updates-problem-with-updatin/69947>\
**Category:** Rails\
**Created:** [September 25, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/collection-management-on-form-updates-problem-with-updatin/69947 "2006-09-25T19:17:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 25, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/collection-management-on-form-updates-problem-with-updatin/69947/1 "2006-09-25T19:17:22Z")

</div>

I have an AR descendant (job.rb) that contains a collection of other AR  
models.

has\_many :target\_lists, :through =\> :job\_list\_associations, :source =\>  
:target\_list

I have a form that I use to update the target\_lists array.

Here are the two methods for “target\_list\_ids” which I use to manage  
the form data for job.

def target\_list\_ids  
ids = Array.new  
self.target\_lists.each do |list|  
ids \<\< list.DataSetID  
end  
ids  
end

public  
def target\_list\_ids=(list\_array)  
#self.target\_lists = Array.new - WHY DOES THIS CAUSE  
PROBLEMS?!!?!!?  
self.target\_lists.clear  
list\_array.each do |id|  
self.target\_lists \<\< TargetList.find(id)  
end  
end

On the post, when target\_list\_ids is called, if I use “clear” on the  
target\_lists array and then set whatever came in on the form,  
everything works fine and I see my changes on the form.

However, if I try to assign self.target\_lists to a new Array object and  
do the setting, I never see the changes. When the form redisplays  
(assuming another error on the page, I see the form without any list  
changes I made).

This _almost_ makes sense to me, but I wondered if someone could  
explain why setting self.target\_lists to be a new Array object seems to  
ensure that none of the changes are stored in the job object. Is it  
because the “attribute” target\_lists is not being updated when I create  
the new array? And if I simply manipulate the existing collection (via  
clear), I’m directly updating the “attribute” target\_lists?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 25, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/collection-management-on-form-updates-problem-with-updatin/69947/2 "2006-09-25T20:38:33Z")

</div>

Does the issue with resetting my collection to a new Array stem from the  
fact that I’m not setting the join attribute on the elements that I’m  
adding to the collection?

The thing about that is this is a _new_ Job object so there isn’t  
anything in the database, so I wouldn’t think that the absence of join  
attributes would be an issue - there shouldn’t be any foreign key values  
anyway.

WG
